How to change owner of lead as current owner in javascript ? - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:

How to change owner of lead as current owner in javascript ?

var LeadToUpdate = new sforce.SObject("Lead"); 
LeadToUpdate.Id = "{!Lead.Id}"; 
LeadToUpdate.OwnerId = '{!$User.Id}'; 

I am using this code but owner not changing.

How I can change owner of lead as current lead ?

Ashutosh TripathiAshutosh Tripathi
Are you running this code on the button click? Why don't you try using apex.
public without sharing class SampleClass{
        public static void changeOwner(Lead led, User us){
             led.OwnerId = us.Id;
             update led;
Without sharing keyword make sure that code runs in system context and so it has full access to change the ownership of record event if current user id not the owner.

If you want to use JS only, then try the below code on the custom button click. The check out the similar old threads.
Jeff MayJeff May
Why would you use Javascript for that?  Process Builder can do it just fine.
Bill PowellBill Powell
Agree w/Jeff. Avoid JS if you can. 

Jeff and Bill Powell, I have to open detail page in edit mode thats why I can not use process builder for that.

How can I achive this ?