How to recall a approved record in Salesforce?

Apex Class:

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);
    }
}


Custom Button Javascript:

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

var r = confirm("Are you sure want to recall the approval process?");
if(r == true)
{
sforce.apex.execute("approvalRecall","recallApproval",{ID:"{!Member__c.Id}"});
alert("Approval has been recalled");
window.open('/{!Member__c.Id}');
}
else
{
alert("Recall Cancelled");
}



 Output:




3 comments:

  1. Thank you for your post! This was really helpful and worked excellent.
    SF ADMIN

    ReplyDelete
  2. This does'nt work for Approved Records ???

    ReplyDelete
  3. Hi,
    I have an approved approval for a form associated with an opportunity(oppty A). We had to move the form to a different opportunity(oppty B). How do I move the approval from OpptyA to OpptyB?. I tried using the workbench and the only object related to approval was the ProcessInstanceWorkitem. But I can't seem to find that processinstance Id in that object.
    Thanks for your help in advance.

    ReplyDelete