+ Start a Discussion
Gabe RothmanGabe Rothman 

Apex trigger to update field on new Lead owner when ownership changes...

Hey all,
I started the process of teaching myself Apex after several years as an advanced admin and I'm having a heck of a time trying to write a simple trigger. I've tried to hack it together based on what I've learned so far and so sample code from other triggers, but I just can't seem to figure it out.  Frankly, I'm embarrassed to post what I have so far because I don't think it's even in the ballpark. 

Can anyone help me sort out how to write this very simple trigger? Here's the use case:

We have a custom checkbox on the Lead object called Claim_Lead__c which is used as part of a claiming process by our reps.  Essentially, because we use round robin assignment sometimes leads get assigned to reps as part of the round robin, but another rep has already been working a lead from the same company (via prospecting, event list upload, etc.). In that case the rep who has been working the lead can claim it by clicking the Claim_Lead__c checkbox and submitting the Lead for approval. If the manager approves the request, the approval process automatically updates the Lead owner to the claiming rep and unchecks the Claim_Lead__c box. Both the Lead owner change and the unchecking of the Claim_Lead__c box happen as part of the final approval action on the approval flow. 

We also have another custom field on the User object called 'Lead_Owner_Reassign_Stamp__c'.  When the owner of a lead changes, if the Claim_Lead__c field also changes from True to False I want a trigger to fire which stamps the new Lead owner's ID into the Lead_Owner_Reassign_Stamp__c text field. 

Thanks in advance for your help!

Gabe RothmanGabe Rothman
As a follow up, conceptually my thought process is as follows, but I just can't seem to figure out the correct syntax
  1. Instantiate a list of Leads
  2. Loop through the list of Leads and find Leads for which both the Owner and Claim_Lead__c fields have changed by using Trigger.new and Trigger.old
  3. Not really sure what the logic of the method that pulls the Owner ID from those leads and inserts it into the User record should look like.