0

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!!

1 Answer 1

1

Your code as shown never calls the recallApproval method.

You'll also need to change the recallApproval method to accept a parameter of type Set<Id> instead of just a single Id value and then modify the code within that method to be bulk-safe. i.e. Not using only the first element in the query result list for the work item.

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

    }

    // call the recallApproval method for these eventIds
    recallApproval(eventIds);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .