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
MnZ321MnZ321 

How to query for accounts in child territories? Unable to find any documentation.

Hi All. How to query for accounts in child territories? I can't seem to find any documentation for this 

Currently I'm able to query for the accounts in the current logged-in user's territrry only, but not from the sibling territories.

Following is my code...
 
public class TrackingPartners{

    public List<orders__c> orders {get; set;}
    public TrackingPartners() {   
    }
    
    
    public list<orders__c> getstart() {

    Map<Id,UserTerritory> UserTerritoryCurrentUserMap = new  Map<Id,UserTerritory>([Select u.UserId, u.TerritoryId, u.IsActive, u.Id  From UserTerritory u Where u.isActive=true and u.userId =: UserInfo.getUserId()]);

    
    
    set<Id> TerritoryIdSet = new set<Id>();
	
    for(UserTerritory ut:UserTerritoryCurrentUserMap.values())
    {
          TerritoryIdSet.add(ut.TerritoryId);
    }    

      list<Group> map_group = [Select Id, RelatedId from Group where (Type='Territory' OR Type='TerritoryAndSubordinates') AND RelatedId IN : TerritoryIdSet];


    List<SYSTEMS__c> lst_PartnersAcc = [SELECT CUST_NUM__c,Account__c
                                                 FROM SYSTEMS__c WHERE Account__c IN                                                  
                                                 (Select  AccountId from AccountShare where ( UserOrGroupId IN : map_group OR  UserOrGroupId =:UserInfo.getUserId()) AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule'))
                                                 ];

                                                 
    Set<String>tempList = new Set<String>();

    for(SYSTEMS__c s : lst_PartnersAcc) {
        tempList.add(s.CUST_NUM__c);

        
    }
    
    List<orders__c> orders =[SELECT Orders__c,id FROM orders__c   
                                 WHERE  Bill_to__c IN: tempList OR
                                        Payer__c IN: tempList OR 
                                        Ship_To__c IN: tempList OR
                                        Sold_to__c IN: tempList
                                     ]; 
                                    

     return orders;                                
  }                                
  
}

How to query accounts from the child territory? Any reference would be great. Thanks in advance! 
Best Answer chosen by MnZ321
Swayam  AroraSwayam Arora
Hi,

Try putting this code after line no 19.
 
List<Territory> childTerritories = [Select Id from Territory where ParentTerritoryID in :TerritoryIdSet];
for(Territory ct : childTerritories) {
        TerritoryIdSet.add(ct.Id);
}

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

Regards,
Swayam
 

All Answers

MnZ321MnZ321
orders__c is a custom object and has no link to standard object order. It has been modified here for demo purpose
Swayam  AroraSwayam Arora
Hi,

Try putting this code after line no 19.
 
List<Territory> childTerritories = [Select Id from Territory where ParentTerritoryID in :TerritoryIdSet];
for(Territory ct : childTerritories) {
        TerritoryIdSet.add(ct.Id);
}

Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.

Regards,
Swayam
 
This was selected as the best answer
MnZ321MnZ321
@swayam You are the best! It worked. Thanks. 
Allen ManamelAllen Manamel
@Swayam,

Since I am very new to Territory concept. I would be very grateful to you if you could help me with this.
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005XFoQAM