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
BhavanaBhavana 

send reminder email to pending current approver

Hi 
Can you please help using batch apex to send approval reminder email to pending approver on Opportunity. I have an approval process setup with few related users as approvers, but to send the reminder email, time-based workflow process won't help as it needs to pick current pending approver name. Is there a way to capture the pending approver name in custom field and use it in workflow to send email alert? Or, can we have a batch class for this?

I have found below query to get the current pending approver name. Please let me know how to use it in batch apex to send reminder email after 48 hours of submitting for approval.

SELECT CompletedDate, ElapsedTimeInDays, ElapsedTimeInHours, ElapsedTimeInMinutes, Id, ProcessDefinitionId, Status, SubmittedById, TargetObjectId FROM ProcessInstance WHERE TargetObjectId = 'aaaaaaaa' And Status ='Pending'

SELECT ActorId,Comments,CreatedById,CreatedDate, ElapsedTimeInDays, ElapsedTimeInHours,   ElapsedTimeInMinutes,Id, OriginalActorId, ProcessInstanceId,  StepNodeId,StepStatus, SystemModstamp FROM ProcessInstanceStep where ProcessInstanceId='bbbbbbb'

Thanks.
Rahul Jain 169Rahul Jain 169
global class SendReminderMail implements Database.Batchable<sobject> {
  
    global LIST<id> userId = new LIST<id>();
    
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
       
        return 
            Database.getQueryLocator('select ActorId, ProcessInstance.TargetObjectId from ProcessInstanceWorkitem where ElapsedTimeInHours > 48');
    }
    global void execute(Database.BatchableContext bc, List<ProcessInstanceWorkitem> scope)
    {
        
         for(ProcessInstanceWorkitem p : scope)
        {
            Schema.SObjectType objectType = p.ProcessInstance.TargetObjectId.getSobjectType();
            
            if(objectType.getDescribe().getName().equals('Opportunity'))
            {
                
                userId.add(p.ActorId);
            }
        }
        
        
        LIST<Messaging.SingleEmailMessage> allMsg = new List<Messaging.SingleEmailMessage>();
        for(Id userid : userId)
        {
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        	mail.setTemplateID('00X7F0000012P3x');
        	mail.setTargetObjectId(userid);
            allMsg.add(mail);
        }
        Messaging.sendEmail(allMsg, false); 
    }
    global void finish(Database.BatchableContext bc)
    {
        
    }
    
}