+ Start a Discussion
SF Beginner 2019SF Beginner 2019 

owner is not updating correcty due to single zip code but multiple country

I have this issue wherein the Account Owner is not updating correctly this is because of duplicate Country in a single zip code, can you help me on my apex class below? How can I identify the zip code and country so that the account owner will of the account will be updated correctly. it has one zip code but it updates between the three countries it first get.
 
public static void insertAccountOwner(Set<Account> accList){
            set<String> TerritoryIds = new Set<String>();
            Map<Id, String> mapAccountTerritory = new Map<Id, String>();
            Set<String> PostalCodes = new Set<String>();
            Set<String> CountryCodes = new Set<String>();
            Set<String> MarketSegments = new Set<String>();
            
            Map<String,String> TerrToAccMap = new Map<String,String>();
            Map<String,String> MolTerrToAccMap = new Map<String,String>();
            Map<String,String> DhlTerrToAccMap = new Map<String,String>();
            Map<String,String> SpgTerrToAccMap = new Map<String,String>();
            Map<String,String> ETeTerrToAccMap = new Map<String,String>();
            Map<String,String> IKAMTerrToAccMap = new Map<String,String>();
            
                     for(Account acc :accList){
                
                //Add account zipcode if not international
                //if(acc.Group__c != 'International') {
                if (acc.ShippingPostalCode != null) PostalCodes.add(acc.ShippingPostalCode);
                   if (acc.ShippingPostalCode == null) {
                       acc.ShippingPostalCode = '';
                       PostalCodes.add(acc.ShippingPostalCode);
                   }
                         
                if (acc.ShippingPostalCode != null && acc.Group__c == 'Corporate' ) PostalCodes.add('99999');
                if (acc.ShippingPostalCode != null && acc.ShippingCountry =='Canada' ) PostalCodes.add(acc.ShippingPostalCode.left(3));
                if (acc.ShippingCountry != null) CountryCodes.add(acc.ShippingCountry);
                if (acc.MarketSegment__c != null) MarketSegments.add(acc.MarketSegment__c);
            }

            if (!PostalCodes.isEmpty()) {
                for(Territory__c t: [SELECT Id, Name, Territory_ID__c, Market_Segment__c, IKAM__c, MOL__C, DHL__C, ZipCode_Unique__c, Country__c, 
                                     RSC__C, ETE__C  FROM Territory__c WHERE Name in :PostalCodes  and Country__c in :CountryCodes LIMIT 4000 ]){
                                         
                                         if(t.Territory_ID__c != null){
                                             TerritoryIds.add(t.Territory_ID__c);
                                         }
                                         if(t.MOL__C != null){
                                             TerritoryIds.add(t.MOL__C);
                                         }
                                         if(t.DHL__C != null){
                                             TerritoryIds.add(t.DHL__C);
                                         }
                                         if(t.RSC__C != null){
                                             TerritoryIds.add(t.RSC__C);
                                         }
                                         if(t.ETE__C != null){
                                             TerritoryIds.add(t.ETE__C);
                                         }
                                         if(t.IKAM__c != null){
                                             TerritoryIds.add(t.IKAM__c);
                                         }
   
                                         String zipMktSeg = t.ZipCode_Unique__c;
                                         TerrToAccMap.put(zipMktSeg, t.Territory_ID__c); 
                                         MolTerrToAccMap.put(zipMktSeg, t.MOL__C);
                                         DhlTerrToAccMap.put(zipMktSeg, t.DHL__C);
                                         SpgTerrToAccMap.put(zipMktSeg, t.RSC__C);
                                         ETeTerrToAccMap.put(zipMktSeg, t.ETE__C);
                                         IKAMTerrToAccMap.put(zipMktSeg, t.IKAM__c);
                                     }
            }
            
            //if (TerritoryIds.IsEmpty()) TerritoryIds.add('99999');
            TerritoryIds.add('99999');
            
            System.debug('@@ TerritoryIds: '+ TerritoryIds);
            Map<String, Id> mapTerritoryUser = new Map<String, Id>();
            
            for(User u: [SELECT Id, Name, Assigned_Territory_ID__c FROM User WHERE (Assigned_Territory_ID__c =:TerritoryIds) LIMIT 200]) {
                mapTerritoryUser.put(u.Assigned_Territory_ID__c, u.Id);
            }
            ///if(!mapTerritoryUser.IsEmpty()) {            
                for(Account acc :accList){
                    String zipMktSegAcc = acc.ShippingPostalCode; 
                    String zipMktSegAcccanada = acc.ShippingPostalCode.left(3); 
                    //if(acc.Group__c != 'International') {
                    //String t = mapAccountTerritory.get(acc.Id);
                    System.debug('@@ zipMktSegAcc: '+zipMktSegAcc);
                    System.debug('@@ zipMktSegAccCAnada: '+zipMktSegAcccanada);
                    String tc = TerrToAccMap.get(zipMktSegAcccanada);
                    String t = TerrToAccMap.get(zipMktSegAcc);
                    String m = MolTerrToAccMap.get(zipMktSegAcc);
                    String d = DhlTerrToAccMap.get(zipMktSegAcc);
                    String s = SpgTerrToAccMap.get(zipMktSegAcc);
                    String e = ETeTerrToAccMap.get(zipMktSegAcc);
                    String k = IKAMTerrToAccMap.get(zipMktSegAcc);
                    //Group is Key Accounts
                    if(acc.Group__c == 'Key Accounts') {
                        // Check if Accoutn Executive is Empty
                        System.debug('@@ acc.Ref_Lab_Dir__c1: '+acc.Ref_Lab_Dir__c);
                        System.debug('@@ acc.IDN_HSD__c1: '+acc.IDN_HSD__c);    
                        if(acc.IDN_HSD__c != null){
                            acc.OwnerId = acc.IDN_HSD__c;
                            System.debug('@@ acc.IDN_HSD__c: '+acc.IDN_HSD__c); 
                            //Check if Ref Lab Director
                        }else if(acc.Ref_Lab_Dir__c != null){
                            System.debug('@@ text test ');
                            System.debug('@@ acc.Ref_Lab_Dir__c: '+acc.Ref_Lab_Dir__c);                          
                            acc.OwnerId = acc.Ref_Lab_Dir__c;
                            
                        }               
                    }else if(acc.Group__c == 'Owned Accounts'){
                        if(t == null) {               
                            t = '99999';
                        } 
                        
                    }else if(acc.Group__c == 'Corporate'){
                        t = '99999';
                    }
                    /*else if(acc.Group__c == 'International'){
                        t = '99999';
                    }*/
                     System.debug('@@ testMap: '+mapTerritoryUser);
                    if(!mapTerritoryUser.isEmpty()){
                        //assign the owner id based on the territory id in territory object
                        if(mapTerritoryUser.containsKey(t) && (acc.Group__c != 'Key Accounts')) {                       
                            acc.OwnerId = mapTerritoryUser.get(t);
                        }
                        if(mapTerritoryUser.containsKey(tc) && (acc.Group__c != 'Key Accounts')) {                       
                            acc.OwnerId = mapTerritoryUser.get(tc);
                        }
                        //assign the Molecular Director based on the Mol Territory id in territory object
                        if(mapTerritoryUser.containsKey(m)) {
                            acc.MD__C = mapTerritoryUser.get(m);
                        }
                        else
                        {
                            acc.MD__C = Null;
                        }
                        //assign the DHI Specialist based on the DHI Territory id in territory object
                        if(mapTerritoryUser.containsKey(d)) {
                            acc.DHL__c = mapTerritoryUser.get(d); 
                        }
                        else {
                            acc.DHL__c = Null;
                        }
                        //assign the SPG Rep on the Reasearch Territory id in territory object
                        if(mapTerritoryUser.containsKey(s)) {
                            acc.SPG__c = mapTerritoryUser.get(s); 
                        }
                        else 
                        {
                            acc.SPG__c = Null; 
                            
                        }
                        //assign the Eye Health based on the Eye Territory id in territory object
                        if(mapTerritoryUser.containsKey(e)) {
                            acc.ETE_Rep__c = mapTerritoryUser.get(e); 
                        }
                        else 
                        {
                            acc.ETE_Rep__c = Null;
                        }
                        if(mapTerritoryUser.containsKey(k)) {
                            acc.Key_Account_Manager__c = mapTerritoryUser.get(k); 
                        }
                        else
                        {
                            acc.Key_Account_Manager__c = Null; 
                        }
                    }
            }