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
Mark R KeyworthMark R Keyworth 

Update custom relationship based on a standard relationship.

Hello there!

Let me preface this by saying that custom coding is well outside of my wheelhouse. That said, I am trying to update the value of a custom relationship field on Leads when the owner of the Lead is changed. My code is not throwing any errors, but it is also not updating the custom relationship as I would expect. I have no idea what I am doing wrong. The code is as follows:
 
trigger leadAutoSAChangeTEST on Lead (after update) {
for( Id leadId : Trigger.newMap.keySet() )
{
    if( Trigger.oldMap.get(leadId).Owner != Trigger.newMap.get(leadId).Owner )
    {
        Lead leadtoUpdate = Trigger.newMap.get(leadId);
        leadtoUpdate.Sales_Agent__c = [SELECT Id FROM Sales_Agent__c
                                      WHERE Name = :Trigger.newMap.get(leadId).Owner.Name
                                      LIMIT 1].Id;
        update leadtoUpdate;
        }
    }
}

 
Van DungVan Dung
Dear Mark,
In this case, I think you should be use "before update". Because, on the "after update" you cannot update the current record.
Hope that can help you.