You need to sign in to do that
Don't have an account?
SF Beginner 2019
The Owner of the account is not updating correctly.
I have this issue wherein the account owner are not updating correctly, wherein, this lookup in a territoty object and user.
ex, the shipping country is united states, but the account owner is germany. how come that happen? I am not sure what to check in here, wherein when I update the record it will update correctly same goes with the batch. are there discrepancies in my apex class? thanks for the help!!
I have this trigger
I have this class
ex, the shipping country is united states, but the account owner is germany. how come that happen? I am not sure what to check in here, wherein when I update the record it will update correctly same goes with the batch. are there discrepancies in my apex class? thanks for the help!!
I have this trigger
trigger TerritoryAssignmentTrigger on Account (before insert, before update) { Set<Account> sIntAccountsCanada = new Set<Account>(); Set<Account> sAccounts = new Set<Account>(); /* if(RecursionBlocker.flag){ if(trigger.isBefore && trigger.isInsert){ AccountTriggerHandler.InsertAccountOwner(trigger.new); } if(trigger.isAfter && trigger.isUpdate){ AccountTriggerHandler.UpdateAccountOwner(trigger.new); } } */ if(trigger.isBefore && trigger.isInsert){ for(Account acc : Trigger.new) { if(acc.ShippingPostalCode != null ){ sAccounts.add(acc); } } } if(trigger.isBefore && trigger.isUpdate){ for(Account acc : Trigger.new) { system.debug(acc); if(acc.ShippingPostalCode != null ){ sAccounts.add(acc); } } } Triggers__c t = Triggers__c.getInstance(); if (t == null || t.Territory__c) { if(!sAccounts.isEmpty()){ AccountTriggerHandler.InsertAccountOwner(sAccounts); } AccountTriggerHandler.insertAccountOwnerCanada(sIntAccountsCanada); } }
I have this class
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; } } }
Background, if you have 3 x accounts with Zip and Country code detail of
1234 AU
1234 US
1234 GB
so your query returns the Territory records correctly, but then you are busting them out into separate Maps of <String,String> and therefore there is no connection between Zips and values and countries. - Only Zip is used as the key.
I would make the following changes.
High level
Remove all the <String,String> maps.
Create a map of <String,Territory__c>.
For the String (key) create a key of country code with ZIP as the unique key.
Create the Map with that key.
Then when querying the Account records, create the Key from the Account details and interrogate the Map.
Regards
Andrew