Hello Friends, In this tutorial, I am providing an example that illustrates, how to submit an Approval process using an Apex trigger and controller. For this, I am using an Approval Process that build on Opportunity object. I am just providing the code sample that an apex trigger and apex controller to call the approval process. This tutorial also contains steps to execute your trigger and submit approval process.

After completing this tutorial, you'll able to,
  • Submit an Approval process through the Apex code.
So let's start,

In this example, I am using an Approval process that executed when opportunity amount is greater than 10,000 also use a custom picklist that contains user-defined steps to submit an Approval process.

Note: Before submitting your approval process, you have set a record that fulfills your approval process entry criteria.

Step 1: Create a custom picklist called "Approval Step" with the following values on the opportunity object.
  • Submit
  • Approve
  • Reject
Step 2: Create an Approval process on opportunity object that you want to submit through apex trigger.

Step 3: Create an Apex controller called "ProcessApprovalRequest" and replace the following code:

ProcessApprovalRequest.apxc
 public class ProcessApprovelRequest {
    /**
    * This method will submit the opportunity for approval
    **/
    public static void submitForApproval(Opportunity opp){        
        // Create an approval request for  Opportunity        
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();          
        req.setComments('Submitting approval request using Trigger');        
        req.setObjectId(opp.id);
        // Submit the approval request for the Opportunity        
        Approval.ProcessResult result = Approval.process(req);        
    }     
    
    /**
    *Get ProcessInstanceWorkItemId using SOQL
    **/
    public static Id getWorkItemId(Id targetObjectId){
        Id workItemId = null;
        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId =: targetObjectId]){
            workItemId  =  workItem.Id;
        }
        return workItemId;
    }

    /**
    * This method will Approve the opportunity
    **/
    public static void approveRecord(Opportunity opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');        
        Id workItemId = getWorkItemId(opp.id); 
        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

    /**
    * This method will Reject the opportunity
    **/
    public static void rejectRecord(Opportunity opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        Id workItemId = getWorkItemId(opp.id);
        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }
 }

Step 4: Create an Apex trigger called "SubmitForApproval" and replace the following code:

SubmitForApproval.apxt
 trigger SubmitforApproval on Opportunity (after insert,after update) {
    for(Opportunity opp:trigger.New){       
        try{
            if(Trigger.isInsert){
                if(opp.Approval_Step__c == 'Submit'){
                    ProcessApprovelRequest.submitForApproval(opp);
                }
                else if(opp.Approval_Step__c == 'Approve'){
                    ProcessApprovelRequest.approveRecord(opp);
                }
                else if(opp.Approval_Step__c == 'Reject'){
                    ProcessApprovelRequest.rejectRecord(opp);
                }
            }
            else if(Trigger.isUpdate){
                Opportunity oppOld=Trigger.OldMap.get(opp.Id);
                if( opp.Approval_Step__c == 'Submit' && oppOld.Approval_Step__c != 'Submit') {
                    ProcessApprovelRequest.submitForApproval(opp);
                }
                else if(opp.Approval_Step__c == 'Approve' && oppOld.Approval_Step__c != 'Approve'){
                    ProcessApprovelRequest.approveRecord(opp);
                }
                else if(opp.Approval_Step__c == 'Reject' && oppOld.Approval_Step__c != 'Reject'){
                    ProcessApprovelRequest.rejectRecord(opp);
                }
            }
        }
        catch(Exception e){
            opp.addError(e.getMessage());
        }
    }
 }

Step 5: Edit you opportunity record, fill data according to your approval process entry criteria and then choose "Submit" option in "Approval Step" picklist and save the opportunity record.


You can see in Approval history related list that presents in the bottom of opportunity record, your record is successfully submitted for approval.



Step 6: Again Edit you opportunity record, fill data according to your approval process entry criteria and then choose "Approve" option in "Approval Step" picklist and save the opportunity record.



Your record is successfully Approved when you see in approval history related list at the bottom of the page.



See also:

Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.