+ Start a Discussion
Oiswarja PyneOiswarja Pyne 

Recall Approval Process through Trigger

Hi All.

I need to unlock a custom object (Events__c) record when an Task is created.
Hence I propose to recall the approval process when the Task is created by using an After Insert Trigger.

Here is the below code...

Task Trigger 
trigger TaskTrigger on Task (after insert) {
    if(Trigger.IsInsert){
         TaskSequenceController.afterInsert(trigger.new);    
    }
    
}


Sequence Controller
public class TaskSequenceController{
     /**
    * Method Name : afterInsert
    * Parameter   : 
    * Return type : None
    * Description : This method is used to perform operation on after Insert event of TaskTrigger
    */
    public static void afterInsert(List<Task> taskList){           
        TaskTriggerOperation.taskOnInsert(taskList);           
    }
    
    
 }



Operation Controller

public class TaskTriggerOperation{
    /**
    * Method Name : taskOnInsert
    * Parameter   : 
    * Return type : None
    * Description : This method is used to perform operation on before Insert event of TaskTrigger
    */
    public static void taskOnInsert( List<Task> taskList){  
        Set<Id> eventIds = new Set<Id>();
      for (Task  tsk : taskList) {
       if(tsk.WhatId != null ) { 
        eventIds.add(tsk.whatId);
System.debug('All Event Ids:' +eventIds);
    } 
   
}
}
    /*
    * Method Name : recallApproval
    * Parameter   : 
    * Return type : None
    * Description : This method is used to perform recall Approval process on Event record
    */
     public static void recallApproval(Id eventIds)    
    {        
       
        List<ProcessInstanceWorkitem> piwi = [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId
        FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: eventIds];
        System.debug('Target Object ID:' +eventIds);
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Recalling and unlocking request.');
        req.setAction('Removed');        
        req.setWorkitemId(piwi.get(0).Id);
        req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Approval.process(req,false);
    } 
    }

I am kinda new to Apex.Aplogies for the amateurish coding.However the code is not working exactly the way I want.Email is sent to user , task is getting created but record is still locked as the approval process in not recalled.I checked the debug logs and found that the Event Id is getting populated correctly at the end of taskonInsert method but no ID is generated in the debug log for Recall Approval methos.I guess I am not passing the parameters correctly in the Recall Approval method.
Can anyone suggest me what to do and where I am missing out?
Thanks in advance!!
sandeep@Salesforcesandeep@Salesforce
Hi, 

What I am thinking is that you should use  Approval.process(req); directly to avoid partially commitment, so in case of any error you will be able to debug it. 

Thanks