• Mark R Keyworth
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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;
        }
    }
}