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
Kumar5Kumar5 

Update Territory field and owner field on account when created or updated

Hi Team,

I am trying to update Territory field and Owner field on account when ever created get created or updated with the below code, 

The custom metadata has 10 records with count,territory and owner to assign.  The below logic is working for single record, but when i load 10 records the last one only getting update.

Could you please help me where i m wrong here 
public class AsAccountTerritory{
    
    Public static void assignRSETerritoryandOwner(List<Account> newList) {
        
        Map<string,string> ASTerritoryCountMap = new map<string,string>(); 
        Map<string,string> ASTerritoryAccountMap = new map<string,string>(); 
        Map<string,string> AccountTerritoryCount = new map<string,string>(); 
        List<string> rseTerritoryList = new List<string>();               
        Map<string,Account> UpdateAccountTerritorycountMap = new map<string,Account>(); 
        string territoryCount;
        
        list<Account> rseTerritoryAccountList = new List<account>();        
        Map<string,Account> UpdateAccountMap = new map<string,Account>();
        
        for(RSTerritory_Account__mdt rselist : [select id,count__c,Territory__c,Territory_Owner__c from AS_Territory_Account__mdt]){
            rseTerritoryCountMap.put(rselist.count__c,rselist.Territory__c);
            rseTerritoryAccountMap.put(rselist.Territory__c,rselist.Territory_Owner__c);  
            rseTerritoryList.add(rselist.Territory__c);             
        }
        
        TerritoryAccountList = [select id from account where Business_Unit__c = 'SMB BU' and Area__c = 'Americas' and Market__C = 'United States'  and Billing_Country__c = 'USA' and  leadscore = '20' and AS_Territory__c =: rseTerritoryList];
        
        integer  rseTerritoryAccountList1 = 10;        
        
        for(integer i =1;i<= newList.size(); i++){  
            for(Account accList : newList){       
                if(accList.LeadScore == '20' && accList.Billing_Country__c == 'USA'){
                    integer newaccountsizewithold  = rseTerritoryAccountList1 + i;
                    territoryCount = string.valueof(MATH.MOD(newaccountsizewithold,10));
                    
                    if(rseTerritoryCountMap.containskey(territoryCount)){                   
                        UpdateAccountTerritorycountMap.put(territoryCount,accList);
                        AccountTerritoryCount.put(territoryCount,territoryCount);
                        
                    }
                }
            }
        }
        
        for(Account accList : UpdateAccountTerritorycountMap.values()){
            if(accList.Leadscore == '20' && accList.Billing_Country__c == 'USA'){
                
                for(String terrcount : AccountTerritoryCount.keyset()){
                    
                    if(rseTerritoryCountMap.containskey(terrcount)){                    
                        UpdateAccountTerritorycountMap.get(terrcount).AS_Territory__c = rseTerritoryCountMap.get(terrcount); 
                        
                        
                        if(rseTerritoryAccountMap.containskey(UpdateAccountTerritorycountMap.get(terrcount).AS_Territory__c)){                    
                            UpdateAccountTerritorycountMap.get(terrcount).Ownerid = rseTerritoryAccountMap.get(UpdateAccountTerritorycountMap.get(terrcount).AS_Territory__c);
                        }
                        
                    }
                }   
                
            }
        }       
    }   
}