function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allen ManamelAllen Manamel 

How to traverse territories in Salesforce using SOQL query -- URGENT

I have a field on contact object called 'Region' (Text formula field) whose end value would be in the range of 1-5. On the other hand, I have a territory model setup in my Salesforce instance. There are 5 territories with the Type Region that come on level 3 in model hierarchy. Here is my territory hierarchy screenshot.

User-added image

I have a field on contact object called 'Region' (Text formula field) whose end value would be in the range of 1-5. On the other hand, I have a territory model setup in my Salesforce instance. There are 5 territories with the Type Region that come on level 3 in model hierarchy. Here is my territory hierarchy Territory hierarchy
Here are the five region type territories

User-added image

What I want is that if the value in Region field is 1 on contact page, it should go to the Region 1 territory and check the assigned users in the region 1. Similarly for other regions. I am taking an example where Region value on contact object is 3. In that case, I want the code to go to Region 3, Traverse the assigned users in region 3 and check the "Role in Territory field" in assigned user.After it checks the "Role in Territory" which will either be Regional Counselor L-Z or Regional Counselor A-K (as shown in the screenshot). Then it compares the "last name" field on the contact object. If the user's last name starts with L-Z then in Region 3(in our example) in the assigned users, the one who has the Role "Regional Counselor L-Z" should be assigned to the "Owner" lookup field on the contact object. if last name starts with A-K then the user with the role "Regional Counselor A-K" should be populated in the "Owner" field on contact object.
Screenshot attached for Region 3. 
User-added image


 
Allen ManamelAllen Manamel

I came up with this so far:

  // Fetch Region 3 data
         
        Territory2 Region3 = ([SELECT Id, Name from Territory2 WHERE Name='Region 3']);  
        System.debug(Region3);
        
        
        // Fetch Assigned Users to Region 3
        
        List<UserTerritory2Association> utList2 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= Region3.Id ]);
        System.debug(utList2);

 

The first part works fine. However, it gives me the following error on the second part
Compile Error: Unexpected token 'Region3.Id'.  

Any help will greatly be appreciated.

Mukesh Kuamr LalMukesh Kuamr Lal
Hi Anjli,

Use this -- WHERE Territory2Id =: Region3.Id
instead of
WHERE Territory2Id= Region3.Id

There is a minor mistake - you missed a : (colon)

It will work fine.
Thanks,
MK Lal
Allen ManamelAllen Manamel
Hi Mukesh,

Thanks for replying back. I will fix it. 

In the meantime, I gave a fixed id to it to test the later part.

// Fetch Assigned Users in Region 3 Territory
        
              List<UserTerritory2Association> UsersInRegion3 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJp']);
              System.debug(UsersInRegion3);
              
              
              for(UserTerritory2Association a: UsersInRegion3)

                 {
   
                              
                   if(contact.UserCategory__c == 'FirstCategory' && a.RoleInTerritory2 == 'Regional Counselor A - K')
      
                        contact.OwnerId = a.UserId;
                   
                   else if (contact.UserCategory__c == 'SecondCategory' && a.RoleInTerritory2 == 'Regional Counselor L - Z')
      
                        contact.OwnerId = a.UserId;
           
   
                }


It does not give me the syntax error but it does not assign the Selected User to the contact owner field. If I just do
contact.Owner = a.UserId; it gives me syntax error.
Any idea how can I fix it?
Allen ManamelAllen Manamel
Also contact is my current instance that I fetched in my code.
 
Mukesh Kuamr LalMukesh Kuamr Lal
Hi Anjali,

Where are you testing it In Trigger or In Anonymous Code ?
Please update asap so that I can help you 

Thanks,
MK Lal
Allen ManamelAllen Manamel
In apex controller
Mukesh Kuamr LalMukesh Kuamr Lal
Ok Anjli, But you didn't used update statement for Contact after assigning the new ownerid.

Do it. it will work.
My Skype Id : mklal.java@gmail.com

Thanks,
MK Lal
Allen ManamelAllen Manamel
Hi Mukesh, 

I have it actually. Here is my code

public pagereference submit() {

//Counselor Assignment Code
         
          if(contact.Region__c == '1')
       
            {
        
        
              // Fetch Assigned Users in Region 1 Territory
        
              List<UserTerritory2Association> UsersInRegion1 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJn']);
              System.debug(UsersInRegion1);
              
              
        
            }
           
          
          else if(contact.Region__c == '2')
       
           {
        
        
        // Fetch Assigned Users in Region 2 Territory
        
              List<UserTerritory2Association> UsersInRegion2 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJo']);
              System.debug(UsersInRegion2);
        
           }
          
          
          else if(contact.Region__c == '3')
       
           {
        
        
        // Fetch Assigned Users in Region 3 Territory
        
              List<UserTerritory2Association> UsersInRegion3 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJp']);
              System.debug(UsersInRegion3);
              
              
              for(UserTerritory2Association a: UsersInRegion3)

                 {
   
                   
           
                   if(contact.UserCategory__c == 'FirstCategory' && a.RoleInTerritory2 == 'Regional Counselor A - K')
      
                        contact.OwnerId = a.UserId;
                   
                   else if (contact.UserCategory__c == 'SecondCategory' && a.RoleInTerritory2 == 'Regional Counselor L - Z')
      
                        contact.OwnerId = a.UserId;
           
   
                }

        
           }
           
           
          else if(contact.Region__c == '4')
       
           {
        
           
        // Fetch Assigned Users in Region 4 Territory
        
              List<UserTerritory2Association> UsersInRegion4 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJq']);
              System.debug(UsersInRegion4);
        
           }
           
           
           
          else if(contact.Region__c == '5')
       
           {
           
         // Fetch Assigned Users in Region 5 Territory
        
              List<UserTerritory2Association> UsersInRegion5 = new List<UserTerritory2Association>([SELECT Id, RoleInTerritory2, Territory2Id, UserId from UserTerritory2Association WHERE Territory2Id= '0MIr00000004CJr']);
              System.debug(UsersInRegion5);
        
           }
           
           
           
          
        
        update contact;
        
        Pagereference Page = new Pagereference('/apex/PCQ_Page_4');
        Page.setRedirect(true);
        return Page;


        
        
    }


Currently I am testing with Region 3 as region 3 field on contact object has  a value 3.
Allen ManamelAllen Manamel
ok so I was reading it here and came to know that OwnerId on Contact object is actually the owner of the account associated with that contact but I am not sure.
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contact.htm?search_text=contact

But I just checked it did not change the owner of the account as well.
Allen ManamelAllen Manamel
Any help?
Mukesh Kuamr LalMukesh Kuamr Lal
Hi Anjli,

Please share your complete code page/controller.
Q. What is the origin of contact that you are using in this code part?
(you are calling submit method from page without any argument and using contact obj directly.)

Also, owner change of Contact is little different from other object.

MK Lal