+ Start a Discussion
ADGADG 

Cannot set Next Approver during an Approval Process through Apex code

I have created an Approval Process and am launching it from an Apex code behind a custom button which is working fine, but the problem is that I am not able to set the Approver from the Apex code (instead it always takes the Approver that is set in the Approval Process.)

Here is what I tried:
    Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
    req.setComments('Submitted for approval. Please approve. Source:ApexClass');
    req.setObjectId(dcr.Id);
    req.setNextApproverIds(new Id[] {'0057E000001dn5T'}); // This is the id of a user
    Approval.ProcessResult result = Approval.process(req);

This submits the record for approval, but sends it to the Approver that is set in the Approval Process, and not to the user with id '0057E000001dn5T' that is set in the code

How can I make the code work? Any help would be appreciated.
Also any other alternative idea to achieve this is very much welcome.
SandhyaSandhya (Salesforce Developers) 
Hi,


​setNextApproverIds(nextApproverIds)
If the next step in your approval process is another Apex approval process, you specify exactly one user ID as the next approver. If not,
you cannot specify a user ID and this method must be null.

The signature is misleading, for it suggests that you can pass a list of approverIds.

public Void setNextApproverIds(ID[] nextApproverIds)

from below link

https://developer.salesforce.com/forums/?id=906F00000008z89IAA

Please refer below links for more information.

http://www.codebycody.com/2016/03/use-apex-code-to-dynamically-select-the-approver-in-a-salesforce-approval-process.html
 
http://salesforce.stackexchange.com/questions/136452/set-the-multiple-user-as-approvers-using-apex-approval-process
 
http://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
ADGADG
Most of the forums I had searched for before posting the question, and also the links above, mention the use of the function setNextApproverIds, but also mentions that this works if the next step in the approval process is another apex approval process, which is not my case. I do not have a next step, I have only one approval step, so could not make this work yet  :(