Achieving bulk approval of records in Salesforce

Bulk Approve

  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);  } });});}
function BulkProcess(){ if (selectedOpportunitiesIdJSON !== '') {  div.classList.remove('slds-hide');  submittingRecords();//first submitting records for approval process.  approvingRecords(); }}
@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(); }}

Bulk Reject

  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);   }  });});}
function BulkProcess(){  if (selectedOpportunitiesIdJSON !== '') {    div.classList.remove('slds-hide');    submittingRecords();
//we will call reject records method
rejectingRecords(); }}
@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(); }}

--

--

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