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
Tyson JoeTyson Joe 

Apex Controller Help: How to change Lead Owner

I've just started into Apex coding and could use some help. I borrowed a Class that someone posted online that allows a user to ACCEPT or REJECT a lead.

Where it changes the Status to 'Working - Accepted' - I want that to be the trigger that CHANGES OWNER to whoever clicked the action. Can someone help me with the script?
public class AcceptLead {
    public Lead l;

    /**
    * The constructor
    *
    * @param controller The standard controller
    */
    public AcceptLead(ApexPages.StandardController controller) {
        this.l = (Lead) controller.getRecord();
    }

    /**
    * The page action that will accept the lead
    *
    * @return The page to goto after loading
    */
    public PageReference pageAction() {
        this.l.Status = 'Working - Accepted';

        update this.l;

        return new PageReference('/' + this.l.Id);
    }
}

 
Best Answer chosen by Tyson Joe
Glyn Anderson 3Glyn Anderson 3
I think you just need to add code similar to Raj V's solution into the original controller action:

<pre>
public class AcceptLead {
    public Lead l;
 
    /**
    * The constructor
    *
    * @param controller The standard controller
    */
    public AcceptLead(ApexPages.StandardController controller) {
        this.l = (Lead) controller.getRecord();
    }
 
    /**
    * The page action that will accept the lead
    *
    * @return The page to goto after loading
    */
    public PageReference pageAction() {
        this.l.Status = 'Working - Accepted';
        this.l.OwnerId = UserInfo.getUserId();    // <== add this line
        update this.l;
 
        return new PageReference('/' + this.l.Id);
    }
}
</pre>
 

All Answers

Raj VakatiRaj Vakati
Here is the simple logic 
trigger LeadUpdate on Lead (before update) { 
for(Lead l :trigger.new){
if(l.status=='Working - Accepted'){
i.OwnerId =UserInfo.getUserId();
}
}

 
Glyn Anderson 3Glyn Anderson 3
I think you just need to add code similar to Raj V's solution into the original controller action:

<pre>
public class AcceptLead {
    public Lead l;
 
    /**
    * The constructor
    *
    * @param controller The standard controller
    */
    public AcceptLead(ApexPages.StandardController controller) {
        this.l = (Lead) controller.getRecord();
    }
 
    /**
    * The page action that will accept the lead
    *
    * @return The page to goto after loading
    */
    public PageReference pageAction() {
        this.l.Status = 'Working - Accepted';
        this.l.OwnerId = UserInfo.getUserId();    // <== add this line
        update this.l;
 
        return new PageReference('/' + this.l.Id);
    }
}
</pre>
 
This was selected as the best answer