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
Rahul Kainth 3Rahul Kainth 3 

How to get old and new values of territories related to an account?

I have a requirement to capture the changes of territories on account update. Suppose if shipping country is getting changed, then territories will get updated. I have to store old value/new value of territory in a new record along with account Id.

The main issue is in the update context. Before update will give me only old territories and after update will give me only new territories. How can i get both new/old territory in a single context?
AbhishekAbhishek (Salesforce Developers) 
Please use the below code snippet, it will work for you.

trigger AccountTerritories on Account (before update){
    if(Trigger.isBefore){
        Set<Id> setOfAccountIds = new Set<Id>();

        for(Account acct : Trigger.New){
            if(Trigger.isUpdate){
                if(acct.Id != null){
                    setOfAccountIds.add(acct.Id);
                }
            }
        }
 
        if(setOfAccountIds.size() > 0){

        /* Declaration of collection data types */
Map<Id, Id> mapOfAccountShare = new Map<Id, Id>();
       Map<Id, Id> mapOfGroup = new Map<Id, Id>();
       Map<Id, Territory> mapOfUserTerritory = new Map<Id, Territory>();


//Query in Account Share object

    List<AccountShare> listOfAccountShare =
    [Select Id, UserOrGroupId, AccountId, RowCause from AccountShare where AccountId IN :setOfAccountIds];
    System.debug('=== contents of listOfAccountShare: ' + listOfAccountShare);

//Map of Account Share
    for(AccountShare acctShare : listOfAccountShare){
    mapOfAccountShare.put(acctShare.Id, acctShare.UserOrGroupId);        
    }      
    System.debug('=== all AccountShare keys in the map: ' + mapOfAccountShare.keySet());  
    System.debug('=== all AccountShare values in the map (as a List): ' + mapOfAccountShare.values());

    //Query in Group object            
    List<Group> listOfGroup = [Select Id, RelatedId from Group where Type='Territory' and Id IN :mapOfAccountShare.Values()];
    System.debug('=== contents of listOfGroup: ' + listOfGroup);
//Map of Group object
    for(Group groupRecord : listOfGroup){
    mapOfGroup.put(groupRecord.Id, groupRecord.RelatedId);        
    }
    System.debug('=== all Group keys in the map: ' + mapOfGroup.keySet());  
    System.debug('=== all Group values in the map (as a List): ' + mapOfGroup.values());

    //Query in Territory object
    Map<Id, Territory> mapOfTerritories =
    new Map<Id, Territory>([select id, name, ParentTerritoryId from Territory where Id IN:mapOfGroup.Values() ]);  
    System.debug('=== all Territories keys in the map: ' + mapOfTerritories.keySet());  
    System.debug('=== all Territories values in the map (as a List): ' + mapOfTerritories.values());  
}
}
}

In case of need, you have to make some minor changes.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
 
Rahul Kainth 3Rahul Kainth 3

Hi Abhishek,

I tried with this code already. This will only give me old values of territories. I want old and new mapping of territories in a single record as well as in single context. If I want new territories, I will get it only in after context.