Recall An Approval Process/Step Using Apex or Lightning Flow in Salesforce : jayakrishnasfdc

Recall An Approval Process/Step Using Apex or Lightning Flow in Salesforce
by: jayakrishnasfdc
blow post content copied from  Jayakrishna Ganjikunta
click here to view original post


Through this Blog post I am sharing Different Approaches of Recalling Approval Process in Salesforce other than doing it from UI.

First and Foremost thing is why do we Recall any approval process instance? We do whenever we submitted the record for approval and we thought we missed to enter all the details and any other specific reason. generally we do go to Record (sObject) in salesforce UI and click on Recall Approval Button.

But if we have more than one/many approval records which need to Recall or if we need to design a custom Recall button instead of using Standard button to update any other automation /Apex whenever its Recalled which cannot achieve by Standard Recall approval button. So lets try to achieve it now.

I am going to do this in 2 different Ways.

  1. From Anonymous Window of Developer Console
  2. Using Apex
  3. Using Lightning Flow

To do these first we need to understand little bit about Process instance model.

ProcessInstance object Represents an instance of an approval process. which is created every time when a record is submitted for approval.

ProcessInstanceHistory object shows all steps and pending approval requests associated with a ProcessInstance.
ProcessInstanceHistory is created every time when record is submitted for approval, reassigned, approved or rejected.

StepsAndWorkItems is the child relationship name for ProcessInstanceHistory under the ProcessInstance object.

1.Recall Approval Process/Record Using Developer Console (Anonymous Window):

Let’s first create an approval process , for example on opportunity.

I Created above Approval process on Opportunity to Just sent for Approval when Amount is Greater than 10,000
Below is the example Opportunity in Approval Queue.

Open Developer Console and Write Below Script.

String recordId = '0060I00000QvbvsQAB'; // Pass the Record id

ProcessInstanceWorkitem[] Pval = [SELECT Id FROM ProcessInstanceWorkItem WHERE ProcessInstance.TargetObjectId = :recordId AND ProcessInstance.Status = 'Pending']; 
if(Pval.size() > 0){
    
    Approval.ProcessWorkItemRequest Preq= new Approval.ProcessWorkItemRequest();
    Preq.setAction('Removed');
    Preq.setWorkItemId(Pval[0].Id);
    Approval.ProcessResult result = Approval.process(Preq);
}

And the Output – Recalled.

2.Recall Approval Process/Record Using APEX

Lets Create an Detail Page Button and call the Apex class from the button to Recall Approval Request.

On Opportunity Object ,create one Detail Page Button with Java script to call Apex.

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var r = confirm("Would you like to Recall Approval Request?");
if(r == true)
{
sforce.apex.execute("approvalRecall","recallApproval",{recId:"{!Opportunity.Id}"});
alert("Approval Request has been recalled");
}
else
{
alert("Recall Request Cancelled");
}

Create an Apex Calss.

global class approvalRecall
{
    webservice static void recallApproval(Id recId)   
    {       
        List<ProcessInstanceWorkitem> piwi = [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: recId];
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setAction('Removed');       
        req.setWorkitemId(piwi.get(0).Id);
  
        Approval.process(req,false);
    }
}

Submit for Approval

Now click on the Button we created ‘Recall Approval Request’ and the Output is

3.Recall Approval Process/Record Using Lightning Flow

I am going to Use a Screen Flow calling Apex Class to Update the Process Instance.
We cannot achieve this with only flow why Because ‘ProcessWorkitemRequest’ is not available in flow.

To use our previously Created apex for this scenario we need to make small change in our apex.

global class approvalRecall
{
   @InvocableMethod
    webservice static void recallApproval(List<Id> recId)   
    {       
        List<ProcessInstanceWorkitem> piwi = [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: recId];
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setAction('Removed');       
        req.setWorkitemId(piwi.get(0).Id);
  
        Approval.process(req,false);
    }
}

I am using @InvocableMethod to display this method in flow. and Update to List for Id parameter.

Lets Create now the Flow.

Get opportunity Id from the button, create a variable with text saying ‘recordId’ and Get the opportunity from Get Opportunity element , Pass the same as in put id in Action.

Now Create Detail Page Button on Opportunity and pass the record id.

And here you go the output result.

Before Recall

Click on Recall from Flow Button

Click on Next
Click on Finish

Thanks for Reading…
Jayakrishna.


April 17, 2021 at 09:45PM
Click here for more details...

=============================
The original post is available in Jayakrishna Ganjikunta by jayakrishnasfdc
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce