Achieving bulk approval of records in Salesforce

Mukul Mahawariya
4 min readNov 6, 2020

To submit lots of records for approval in Salesforce, you need custom functionality.

To understand this, I have written another blog where I have thoroughly explained —

What is the Approval Process?

Why we need bulk approval of records?

How we can achieve this?

Here is the link to the blog “Achieving bulk approval of records in Salesforce”.

Now, we will see how the records are being approved/rejected.

Bulk Approve

If you want you can create a separate VisualForce page for the approval button.

Follow my previous blog to create a button on the Opportunity object, a VisualForce page, and an Apex Controller for bulk approval.

Note — You cannot re-submit the record for the approval process.

Below are the steps to approve record(s) -

  1. Add the following functions to the script of the previous VisualForce page.
async function approvingRecords() { try {  let promise = await approveRecords();  if (!div.classList.contains('slds-hide')) {  div.classList.add('slds-hide'); }  redirectToListView(); } catch (exception) {   if (!div.classList.contains('slds-hide')) {   div.classList.add('slds-hide'); } redirectToListView(); }}function approveRecords() {return new Promise(function (resolve, reject) {Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.BulkApprovalController.approveRecords}', selectedOpportunitiesIdJSON,   function (response, event) {  if (response == 'success') {    resolve(response);  }  else {   reject(response);  } });});}

2. Replace function BulkProcess with the following function -

function BulkProcess(){ if (selectedOpportunitiesIdJSON !== '') {  div.classList.remove('slds-hide');  submittingRecords();//first submitting records for approval process.  approvingRecords(); }}

3. Add the following function to the Apex controller-

@RemoteActionglobal static String approveRecords(String toApproveOppty){ try{  List<Contact> toApproveOpptyList =   (List<Contact>)JSON.deserialize(toApproveOppty,   List<Contact>.class);  List<Approval.ProcessWorkitemRequest> requests = new   List<Approval.ProcessWorkitemRequest>();  List<ProcessInstanceWorkitem> workItems = [SELECT Id,   ProcessInstanceId FROM ProcessInstanceWorkitem WHERE  ProcessInstance.TargetObjectId IN :toApproveOpptyList ];//getting records submiited to the approval process  for(ProcessInstanceWorkitem workItem : workItems){  Approval.ProcessWorkitemRequest req = new    Approval.ProcessWorkitemRequest();  req.setWorkitemId(workItem.Id);  req.setAction('Approve');  req.setComments('Approved');  requests.add(req);} Approval.ProcessResult[] processResults =  Approval.process(requests); return 'success'; }catch(Exception ex){  return ex.getMessage(); }}

ProcessInstanceWorkitem — Represents a user’s pending approval request. We are using to get the Id of the approval process of all Opportunity records submitted for approval.

ProcessWorkitemRequest — Use the ProcessWorkitemRequest class for processing an approval request after it is submitted. We are creating a list of requests and processing them.

4. Now navigate to the Opportunity List View select record(s) and click the Bulk Approve button -

You will see that records Approval Status is changed to Approved.

Bulk Reject

For bulk reject we are going to follow the same steps as above with little changes as follows -

  1. Add the following function to the VisualForce page -
async function rejectingRecords() { try {   let promise = await rejectRecords();   if (!div.classList.contains('slds-hide')) {     div.classList.add('slds-hide');   }  redirectToListView(); } catch (exception) {   if (!div.classList.contains('slds-hide')) {    div.classList.add('slds-hide');   }   redirectToListView();  }}function rejectRecords() {return new Promise(function (resolve, reject) {Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.BulkApprovalController.rejectRecords}', selectedOpportunitiesIdJSON, function (response, event) {   if (response == 'success') {     resolve(response);   }   else {    reject(response);   }  });});}

2. Following changes to BulkProcess function -

function BulkProcess(){  if (selectedOpportunitiesIdJSON !== '') {    div.classList.remove('slds-hide');    submittingRecords();
//we will call reject records method
rejectingRecords(); }}

3. Add the following function to the Apex controller-

@RemoteActionglobal static String rejectRecords(String toReject){  try{   List<String> toRejectOpptyList = (List<String>)JSON.deserialize(toReject, List<String>.class);   List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();   List<ProcessInstanceWorkitem> workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId IN :toRejectOpptyList ];   for(ProcessInstanceWorkitem workItem : workItems){    Approval.ProcessWorkitemRequest req = new   Approval.ProcessWorkitemRequest();    req.setWorkitemId(workItem.Id);    req.setComments('Rejected');    req.setAction('Reject');     requests.add(req);  }  Approval.ProcessResult[] processResults =  Approval.process(requests);  return 'success'; }catch(Exception ex){  return ex.getMessage(); }}

4. Now navigate to the Opportunity List View select record(s) and click the Bulk Reject button -

You will see that records Approval Status is changed to Reject

Conclusion -

I have explained to you why bulk approval is required and how we can achieve it with custom logic.

❖ Things to remember -

First, create an approval process. Second thing, you cannot submit the record(s) again for the approval process.

Keep in mind that we are first submitting records for approval then performing either the approved action or the reject action.

--

--

Mukul Mahawariya
Mukul Mahawariya

Written by Mukul Mahawariya

4x Salesforce Certified | Trailhead Ranger | Salesforce Enthusiast

Responses (1)