Salesforce bulk approval process

Achieving bulk approval of records in Salesforce

  1. Create an approval process on whatever object you like and activate it. I am going to use the opportunity object with a custom picklist Approval Status to show status.
  2. Create a Visualforce Page and an Apex Controller with the following details -
<apex:page standardController="Opportunity" recordSetVar="Opportunity" extensions="BulkApprovalController" lightningStylesheets="true"><apex:slds /><html xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink">  <div class="slds-scope">    <div class="demo-only slds-hide" id="spinner"   style="height:6rem">      <div class="slds-spinner_container">         <div role="status" class="slds-spinner slds-spinner_medium slds-spinner_brand">            <span class="slds-assistive-text">Loading</span>              <div class="slds-spinner__dot-a"></div>                <div class="slds-spinner__dot-b"></div>         </div>      </div>     </div>    </div>    <apex:form >      <apex:actionFunction action="{!redirectToListView}"   name="redirection">      </apex:actionFunction>    </apex:form></html><script type="text/javascript">let selectedOpportunitiesIdJSON = '{!selectedOpportunitiesIdJSON}';let div = document.getElementById('spinner');BulkProcess();function BulkProcess(){ if (selectedOpportunitiesIdJSON !== '') {   div.classList.remove('slds-hide');   submittingRecords(); }}async function submittingRecords() { try {    let promise = await submitForApproval();   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 redirectToListView() {  redirection();}// submitting records using visualforce remotingfunction submitForApproval() {    return new Promise(function (resolve, reject)  {Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.BulkApprovalController.submitForApproval}', selectedOpportunitiesIdJSON, function (response, event) {    if (response == 'success') {      resolve(response);    }    else {       reject(response);    }   });  });}</script></apex:page>
global with sharing class   BulkApprovalController { public List<Id> selectedOpportunitiesId; public String selectedOpportunitiesIdJSON{get;set;} public BulkApprovalController(Apexpages.StandardSetController  standardController){ selectedOpportunitiesId   = new List<Id>(); for(Opportunity oppty:    (List<Opportunity>)standardController.getSelected()){  selectedOpportunitiesId.add(oppty.Id);  // you   will get only Ids from controller. } if(selectedOpportunitiesId.size()>0){   selectedOpportunitiesIdJSON =    JSON.serialize(selectedOpportunitiesId); }}@RemoteActionglobal static String submitForApproval(String   selectedOpportunities){  try{    List<String> selectedOpptys   = (List<String>)JSON.deserialize(selectedOpportunities,  List<String>.class);    List<Approval.ProcessSubmitRequest>   approvalRequests = new   List<Approval.ProcessSubmitRequest>();    //Creating an approval requests for each Opportunity using  ProcessSubmitRequest    Approval.ProcessSubmitRequest approvalRequest;    for(Id opptyId: selectedOpptys){      approvalRequest   = new Approval.ProcessSubmitRequest();      approvalRequest.setComments('Submitting   request for approval');      approvalRequest.setObjectId(opptyId);// which record to approve//change approver id       approvalRequest.setNextApproverIds(new   Id[]      {UserInfo.getUserId()});       approvalRequests.add(approvalRequest);   }// Submit the approval request    Approval.ProcessResult[] result   =    Approval.process(approvalRequests);   return 'success';  }catch(Exception ex){     return 'error '+ex.getMessage();  }}public Pagereference redirectToListView(){    Schema.DescribeSObjectResult destination;    PageReference pageRef;    try{      destination = Opportunity.SObjectType.getDescribe();      pageRef = new PageReference('/' + destination.getKeyPrefix()     );      pageRef.setRedirect(true);      return pageRef;    }catch(Exception ex){      return pageRef;    }  }}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mukul Mahawariya

Mukul Mahawariya

10 Followers

4x Salesforce Certified | Trailhead Ranger | Salesforce Enthusiast