+ Start a Discussion
Shawn Low 37Shawn Low 37 

Creating a Button that changes record owner to current User

I have a unique situation (I think so at least)
We have the Default Lead Setting to "Private" and assign Permission Sets to our internal sales reps for "Read" access. This allows the Reps to Search, and see a listing of all the Leads in the System.
The Scenario:
Sales Rep has lead assigned to them (They are the Lead owner).
Lead calls in and the rep is out on Vacation, so another Rep answers.
New Sales Rep searches SFDC, finds the Lead and clicks a custom button called "Claim Lead".
The Lead is then assigned to them (The new Sales Rep becomes the Lead Owner).
I was thinking to use a Button to call a Process or a Flow, but I can't seem to find any Trailhead classes or anything that would show me how to do this. (I'm just learning Dev stuff, like Lightning Components, VisualForce, etc)
Help please!!!
Thank you
NagendraNagendra (Salesforce Developers) 
Hi Shawn,

Sorry for this issue you are facing.

May I suggest you please try the sample code below and tweak it as per the requirement which might help you further.
trigger UpdateChangeOwner on Lead (after insert) {

    List<Lead> cList = new List<Lead>();

    for(Lead c : trigger.new) {
        c.Change_Owner__c = userInfo.getUserId()


        update cList;

For more information please refer to below link. Hope this helps.

Shawn Low 37Shawn Low 37
Good Morning Nagendra
Thank you for writing out that code for me, I really appreciate it.
Is this a Trigger or Code I can connect to a button or something? (Sorry, Im just starting to learn and understand code.)