+ Start a Discussion
TenaTena 

Apex create an Approval Process

I have some logic that determines if a approval process needs to be kicked off for an Opportunity that I build into Apex code.  I don't see how I can send the approval process to certain people(may be more than one) and then kick off another one based on certain criteria.  I have code that kicks off an approval process but not one that I choose by name, meaning if I create "First Approval Process" how do I kick off that one without having to create criteria under "Approval Process" section for the "First Approval Process"?  Do I even need to create a Approval Process in order to initiate approval process or can I do all of it through Apex code.

 

Here is what I have that kicks off an Approval Process that I have created with criteria but I find in cumbersome that I can't choose which approval process to kick off.

              //create the new approval request to submit    
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            //set the approvers, can only be one :(
            req.setNextApproverIds(approversId);
            req.setComments('Submitted for approval. Please approve.');
              req.setObjectId(EachFlowOpportunity[i].Id);
              // submit the approval request for processing.  How do I choose to kick off "First Approval Process"
              Approval.ProcessResult result = Approval.process(req);

Ritesh AswaneyRitesh Aswaney

You cannot choose which specific Approval Process to submit an Approval Request to.

 

The submitted records are submitted to Approval Processes, in the order they are arranged, until a match is found on the Entry Criteria for one of the Approval Processes.

 

You can work around this by say setting a field before submitting a field on the record before submitting for approval. This field update can update the field to such a value so as to match the Entry Criteria of the Approval Proess that you want to submit to.

 

As for the Approvers, you can choose to assign it to User Lookups populated on the Record submitted for Approval.

 

 

Brian RomanowskiBrian Romanowski
Have you thought about putting the logic into a formula field and then have multiple approval processes that are selected based on that field?