+ Start a Discussion
Saad Ahmad 27Saad Ahmad 27 

Trigger to identify parent territory

I have a list of territories names that are inserted into a custom field, Territories__c from an external system via dataloader. The names are split by a comma. eg: 1234,2345,9564 etc. Another custom field called RVP_Code__c stores the parent territory name. e.g. 1111

I'd like to go thru the list of these territories and add the territories names that don't have the same parent territory into a new field, Sales_Rep_Region__c, for review. I wrote a trigger below but it doesn't seem to be working. Would love to get some help regarding this. Thank you in advance!! 
trigger AccountTerritoryReview on Account (before insert) {
	
    List<Account> RepstoAdd = new List<Account>();
    
    if(TriggerHandler.run ){
        TriggerHandler.run = false;
        
        for (account acc : Trigger.new){
            if (acc.Territories__c != null) {
                for (string t : acc.Territories__c.split(',')) {
                    List<Territory2> tt = new List<Territory2>([SELECT Name,Territory2.ParentTerritory2.Name FROM Territory2 WHERE Territory2.Name = :t]);
                    for (Territory2 tx : tt){
                        if (tx.ParentTerritory2.Name != acc.RVP_Code__c){
                            acc.Sales_Rep_Region__c = tx.Name;
                            RepstoAdd.add(acc);
                        }
                    }
                    }                       
                }
            }
            
            
        }
    
    update RepstoAdd;
    
}


 
Best Answer chosen by Saad Ahmad 27
Duke_SfdcDuke_Sfdc
Hello Saad Ahmad 27

First, you don't need to perform DML operations when you are working with before events in Apex Trigger.

Please see the below code and let me know if you still have some query.

trigger AccountTerritoryReview on Account (after Insert, after update) {
    
    List<Account> accountUpdatedList = new List<Account>();
    Map<string,string> territoryWithParentTerritoryNameMap = new map<string,string>();
    for(Territory2 tr : [SELECT id, Name,Territory2.ParentTerritory2.Name FROM Territory2 
                         WHERE Territory2.ParentTerritory2.Name != Null Limit 50000]){
        territoryWithParentTerritoryNameMap.put(tr.name,tr.ParentTerritory2.Name);
    }
    
    for(Account ac : Trigger.new){
        if(ac.Territories__c != Null && ac.RVP_Code__c != Null){
            for(string str : ac.Territories__c.split(',')){
                if(territoryWithParentTerritoryNameMap.containsKey(str)){
                    string tempParentTerritoryName = territoryWithParentTerritoryNameMap.get(str);
                    if(ac.RVP_Code__c != tempParentTerritoryName){
                        ac.Sales_Rep_Region__c = str;
                        accountUpdatedList.add(ac);
                    }
                }
            }
        }
    }
    
    if(accountUpdatedList.size() > 0)
        DataBase.Update(accountUpdatedList);
    
}

Thanks
Duke_SFDC
 

All Answers

Duke_SfdcDuke_Sfdc
Hello Saad Ahmad 27

First, you don't need to perform DML operations when you are working with before events in Apex Trigger.

Please see the below code and let me know if you still have some query.

trigger AccountTerritoryReview on Account (after Insert, after update) {
    
    List<Account> accountUpdatedList = new List<Account>();
    Map<string,string> territoryWithParentTerritoryNameMap = new map<string,string>();
    for(Territory2 tr : [SELECT id, Name,Territory2.ParentTerritory2.Name FROM Territory2 
                         WHERE Territory2.ParentTerritory2.Name != Null Limit 50000]){
        territoryWithParentTerritoryNameMap.put(tr.name,tr.ParentTerritory2.Name);
    }
    
    for(Account ac : Trigger.new){
        if(ac.Territories__c != Null && ac.RVP_Code__c != Null){
            for(string str : ac.Territories__c.split(',')){
                if(territoryWithParentTerritoryNameMap.containsKey(str)){
                    string tempParentTerritoryName = territoryWithParentTerritoryNameMap.get(str);
                    if(ac.RVP_Code__c != tempParentTerritoryName){
                        ac.Sales_Rep_Region__c = str;
                        accountUpdatedList.add(ac);
                    }
                }
            }
        }
    }
    
    if(accountUpdatedList.size() > 0)
        DataBase.Update(accountUpdatedList);
    
}

Thanks
Duke_SFDC
 
This was selected as the best answer
Saad Ahmad 27Saad Ahmad 27
Thanks Duke_SFDC! I do get the following error when trying to save an account record: 

Apex trigger AccountTerritoryReview caused an unexpected exception, contact your administrator: AccountTerritoryReview: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Trigger.AccountTerritoryReview: line 25, column 1