How can we handle Approval Process using APEX?

Approval process is an automated process which is used to submit, approve and reject records in Salesforce. It specifies the steps necessary for a record to be approved and who must approve it at each step.

We can configure approval process from Salesforce, and then use APEX to submit, approve or reject the record.

Automatic Submission of Record for Approval

For e.g. We want to submit a record for an Opportunity once Amount field exceeds $2000.

First we need to define an approval process on Opportunity and then write an apex trigger which will submit the opportunity for approval.

  1. Create an Approval process on Opportunity.
  2. Create an Apex trigger on Opportunity.

APEX trigger code:

trigger ApprovalOppTrigger on Opportunity (after insert, after update) {
 
 //Request for all opportunity 
 Approval.ProcessSubmitRequest [] requestList = new Approval.ProcessSubmitRequest []{};
 
 for( Opportunity opp : trigger.new ){
 
 //If amount is greater than 2000, go for apporval
 if(opp.Amount> 2000){
 
 // Create an approval request for the opportunity
 Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
 req1.setComments('Submitting request for approval.');
 req1.setObjectId(opp.id);
 
 // Submit the record to specific process
 req1.setProcessDefinitionNameOrId('Opportunity Amount Process');
 }
 
 }
 
 if(requestList.size()>0)
 // Submit the approval request for the opportunity
 Approval.ProcessResult[] result = Approval.process(requestList);
 
}

Approve Record

For e.g: We want to approve the Opportunity record submitted by previous example.

We create a method and pass one parameter of Opporutnity object  which we want to approve.

 /*
 * This method will Approve the opportunity
 */

 public void approveRecord(Opportunity opp)
 {
 //Class used for Approving Record
 Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
 req.setComments('Approving request for Opportunity');
 
 //Approve or Reject Record
 req.setAction('Approve');
 
 //Getting Work Item Id
 ProcessInstanceWorkitem pItem = [Select Id from ProcessInstanceWorkitem 
 where ProcessInstance.TargetObjectId =: opp.id];

 req.setWorkitemId(pItem.Id);
 // Submit the request for approval
 Approval.ProcessResult result = Approval.process(req);
 
 }

 

Reject Record

Rejecting a record is similar to approving record.

We just need to change below line of code in previous example:

//Reject Record
 req.setAction('Reject');

 

Capturing Approval comments using APEX

We can get approval comments for approved/rejected opportunity using “ProcessSteps” standard objects in below APEX code:

//Getting Approval Comments
 Opportunity opp = [Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from Opportunity where Id = :opptyId];

String Comment = opp.ProcessSteps[0].comments;

OR

//process all comments
for(ProcessInstanceHistory ps : opp.ProcessSteps){
  System.debug(ps.comments); 
}

 

Additional Resources

There are several approval objects mentioned below:-

ProcessInstance An instance of a single, end-to-end approval chain.
ProcessInstanceHistory History of changes to a process instance.
ProcessInstanceStep One step in an approval workflow process instance.
ProcessInstanceWorkitem A pending approval request to a specific user.

 
Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s