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
Chamil MadusankaChamil Madusanka 

Create an approval process in apex class

Hi All,

 

I want to create a approval process and assign some specific users to that approval process in apex class. I tried as follows;

 

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(newDPCD.id);
req1.setNextApproverIds(idList); //idList is a list of contact ids

 

But It didn't work. The main issue is I want to assign specific users dynamically.

 

Is there any work around? If someone have any idea or sample code please share it.

 

Thanks in Advance

Shashikant SharmaShashikant Sharma

Change your code to

 

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(newDPCD.id);
req1.setNextApproverIds(idList); 
Approval.ProcessResult result = Approval.process(req1);

let me know with your results

Chamil MadusankaChamil Madusanka

Hi,

 

Thank you for your reply shashikant. I tried that changes. But I got the following exception

 

System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process. [] Trigger.approveTrainingRequest: line 92, column 54: [] 

 

This line 92 is at Approval.ProcessResult result = Approval.process(req1);

 

What is the issue? any solution?

 

Thanks in Advance



 

 

Shashikant SharmaShashikant Sharma

Once an Item is pending for approval then it can not be send to approval till it comes out of approval queue. You have to check whether an Item is in approval or not before sending it for approval.

 

vibrationvibration

How to create a IDlist?

Sagar7990Sagar7990

I get this error :- Error: REQUIRED_FIELD_MISSING. The required field in the only active approval process is that of setting the next approver because the approval process has the condition of 'Let the submitter choose the approver manually.' So the approver set using setNextApproverIds is not being recognised. Any idea ?

surenssurens

instead of idList assign particular user id or user lookup field

 

example:

 req1.setNextApproverIds('0000894059090');