function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4 

recall approval process after batch process

Case Scenario: An approver receives an email when the record is submitted for the approval process.
Batch process on that object changes the status of the field to expired when expiration dates are today.
Approver being very lazy did not approve the record before it got expired. So he approved an expired record.
How to restrict the user from approving the record when the status of the field is expired.
 Here is the trigger I have implemented, though it is not firing as expected when batch process expires. The trigger should kick in to recall the approval process.
 
 
A Future method in a utility class
 @future
    public static void recallRARApproval(Set<String> setRARId){
        if(!setRARId.isEmpty())
        {
             List<Rebate_Authorization_Request__c> rarList = [SELECT Id,RAR_Status__c FROM Rebate_Authorization_Request__c WHERE Id IN:setRARId];
            
            if(rarList != null && !rarList.isEmpty())
            {
                List<ProcessInstanceWorkitem> piwi = [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId
                                                      FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: rarList];
                System.debug('Target Object ID:' + piwi.get(0).Id);
                Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                req.setComments('Recalling and unlocking request.');
                req.setAction('Removed');        
                req.setWorkitemId(piwi.get(0).Id);
                
                Approval.process(req,false);
            }
        }
        
    }
Code from Trigger :
// Recall the Approval Process when PAR is Expired by Batch Process.
            Set<String> setRARIdToRecall = new Set<string>();
            for(Rebate_Authorization_Request__c rar : trigger.new){
                if(rar.Account_Name__c != null && trigger.oldmap.get(rar.id).Account_Name__c != rar.Account_Name__c)
                    mapRarPrimaryAcc.put(rar.Account_Name__c,rar.id);
                
                system.debug('RAR Status--->'+ rar.RAR_Status__c);
                if(trigger.newmap.get(rar.id).RAR_Status__c == 'Expired' && trigger.oldmap.get(rar.id).RAR_Status__c == 'Pending Approval')
                    setRARIdToRecall.add(rar.id);
                
            }
            if(System.IsBatch() == false && System.isFuture() == false){ 
                
                If(!setRARIdToRecall.isEmpty()){
                    UnlockUtillClass.recallRARApproval(setRARIdToRecall);
                    System.debug('Calling from recall approval trigger ');
                }
            }
            
Can some expert guide me what is the mistake?
Thanks