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
Marie KagayMarie 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);
            }
        }
    }
 }



 
Best Answer chosen by Marie Kagay
Deepak Kumar ShyoranDeepak Kumar Shyoran
Try to set OwnerId not Owner to update contact Ownner based on Custom setting.

All Answers

Phillip SouthernPhillip Southern
Marie, are you getting any errors when running the trigger?  Are you trying to set OwnerId field?
Arthur LockremArthur Lockrem
You may want to add con.Id to the debug statement to ensure you are getting the updates you are expecting to the proper records.  If your debug results show the owner is updating for the expected id, and the results are not shown on the record you likely have another process executing.  I would check for another trigger on the contact object or a workflow rule.
Deepak Kumar ShyoranDeepak Kumar Shyoran
Try to set OwnerId not Owner to update contact Ownner based on Custom setting.
This was selected as the best answer
Marie KagayMarie Kagay
Thanks Deepak, that totall worked! I really appreciate the help!