You need to sign in to do that
Don't have an account?
Marie Kagay
Before Trigger Referencing Custom Setting Not Updating Properly
I'm attempting to set the Contact Owner based on the Account Billing Country using a Custom Setting, Lead_Assignment__c, that maps Country to a User Id. When a Contact is created or updated, the trigger pulls the mapped Country and User Id values from the Custom Setting, finds the Billing Country value from the Account associated with the Contact, and sets the Contact Owner with the User Id that correlates with the Country value that matches the Billing Country.
When I run my trigger, the bolded system.debug show that the Contact Owner values is being set correctly (i.e. the User Id is the User Id mapped to the Billing Country in the Custom Setting), but the field is ulitmately not updated with the correct User Id. Based on field history tracking, it does not look like its being updated at all.
Am I missing something on this trigger?
trigger AssignContact on Contact (before insert, before update) {
Set<String> accountSet = new Set<String>();
Set<Id> contactIdSet = new Set<Id>();
List<Lead_Assignment__c> lstBillingOwner = [Select ID,Country__c, New_Owner_ID__c FROM Lead_Assignment__c];
Map<String,String> mpCountryOwner = new Map<String,String>();
for(Lead_Assignment__c l1 : lstBillingOwner) {
mpCountryOwner.put(l1.Country__c, l1.New_Owner_ID__c);
}
Map<Id,User> mpUser = new Map<Id,User>([Select ID FROM User Where ID IN :mpCountryOwner.values()]);
Set<Id> accIds = new Set<Id>();
for(Contact con : trigger.new){
accIds.add(con.AccountId);
}
Map<Id,Account> mpAcc = new Map<Id,Account>([Select ID, Name, BillingCountry FROM Account Where ID IN :accIds]);
for(Contact con : trigger.new){
Account act = mpAcc.get(con.AccountId);
if(con.AccountId != null && act.BillingCountry != null){
if(mpCountryOwner.get(act.BillingCountry) != null){
con.owner = mpUser.get(mpCountryOwner.get(act.BillingCountry) );
System.debug('***** - Updating Owner - New Owner - '+con.owner);
}
}
}
}
When I run my trigger, the bolded system.debug show that the Contact Owner values is being set correctly (i.e. the User Id is the User Id mapped to the Billing Country in the Custom Setting), but the field is ulitmately not updated with the correct User Id. Based on field history tracking, it does not look like its being updated at all.
Am I missing something on this trigger?
trigger AssignContact on Contact (before insert, before update) {
Set<String> accountSet = new Set<String>();
Set<Id> contactIdSet = new Set<Id>();
List<Lead_Assignment__c> lstBillingOwner = [Select ID,Country__c, New_Owner_ID__c FROM Lead_Assignment__c];
Map<String,String> mpCountryOwner = new Map<String,String>();
for(Lead_Assignment__c l1 : lstBillingOwner) {
mpCountryOwner.put(l1.Country__c, l1.New_Owner_ID__c);
}
Map<Id,User> mpUser = new Map<Id,User>([Select ID FROM User Where ID IN :mpCountryOwner.values()]);
Set<Id> accIds = new Set<Id>();
for(Contact con : trigger.new){
accIds.add(con.AccountId);
}
Map<Id,Account> mpAcc = new Map<Id,Account>([Select ID, Name, BillingCountry FROM Account Where ID IN :accIds]);
for(Contact con : trigger.new){
Account act = mpAcc.get(con.AccountId);
if(con.AccountId != null && act.BillingCountry != null){
if(mpCountryOwner.get(act.BillingCountry) != null){
con.owner = mpUser.get(mpCountryOwner.get(act.BillingCountry) );
System.debug('***** - Updating Owner - New Owner - '+con.owner);
}
}
}
}
All Answers