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
karunakaran vkarunakaran v 

Batch class for Email remainder(complicated)

Hi , All  i want to send Email remainder to 2  user when they are not approving the approval process within 24hrs for one user and 48 hrs for another user if they are not approving after that also send them email remainder 1hr once 
Thanks in advance
// Custom object = TestApproval__c
Date/Time = Approval_Submission_Date__c
Date = date1__c
 
Kumaresan.ManickamKumaresan.Manickam
Dear Karunakaran,

Here are the suggestions:
1) Create a batch apex which can be scheduled every hour. 
2) Apex provides approval process instances and its submitted approval steps through objects 'ProcessInstance' & 'StepsAndWorkitems' objects which is child to processInstance record. Iterate over 'StepsAndWorkitems' records for each approval instance reocrds and find if the 'ActorId', 'StepStatus' and submission date. Based on StepStatus value and compare the approval step submitted date the current time. If it matches with more than 24 hours send a email notification from batch itself via apex single email message class.
3) Example SOQL for core logic you are looking for: 
List<ProcessInstance> ProcessInstanceRecord=[SELECT TargetObjectId,LastActorId,LastActor.Name,ProcessDefinitionId,
                                                    ProcessDefinition.developername,
                                                    (SELECT Id, ActorId,OriginalActor.Name,StepStatus,Comments,
                                                    ProcessInstanceId,CreatedDate FROM 
                                                    StepsAndWorkitems Order BY CreatedDate DESC Limit 1) FROM ProcessInstance 
                                                    WHERE TargetObjectId=:c.id]; 
Refer the online articles: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_processinstanceworkitem.htm

Hope this is useful. If you feel this is useful mark this reply as best answer.

Thanks.
karunakaran vkarunakaran v

Dear Kumaresan.Manickam
 Here is the code i have done its not sending email correctly.
global class emailalertforapproverpending implements Database.Batchable<sobject> {
  
    global LIST<id> userId = new LIST<id>();
    
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
       system.debug('test');
        return 
            Database.getQueryLocator('select ActorId, ProcessInstance.TargetObjectId from ProcessInstanceWorkitem where ElapsedTimeInMinutes > 1');
    }
    global void execute(Database.BatchableContext bc, List<ProcessInstanceWorkitem> scope)
    { 
        system.debug('quqery'+bc);
        system.debug('scope'+scope);
         for(ProcessInstanceWorkitem p : scope)
        {
            Schema.SObjectType objectType = p.ProcessInstance.TargetObjectId.getSobjectType();
            
            if(objectType.getDescribe().getName().equals('TestApproval__c'))
            {
                
                userId.add(p.ActorId);
            }
        }
        
        system.debug('UserId---------------->'+ userId );
        
        list<string> s=new list<string>();
       // string u='SELECT Email FROM User where id = userId';
        s.add('SELECT Email FROM User where id = userId');
        //system.debug('email'+u);
        //insert s;
        
        LIST<Messaging.SingleEmailMessage> allMsg = new List<Messaging.SingleEmailMessage>();
        for(Id userr : userId)
        {
            System.debug('All User-------->'+ userr);
          String[] sendingTo = new String[]{'kvinayagam@demandblue.com'};
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            //mail.setTemplateID('00X7F0000012P3x');
            //mail.setTargetObjectId(userr);
            mail.setSubject('Test Approval Remainder !');
            mail.setToAddresses(sendingTo);
            mail.setPlainTextBody('Check the all pending Approvals');
            allMsg.add(mail);
        }
        System.debug('all msg'+allMsg);
        Messaging.sendEmail(allMsg, false); 

    }
    global void finish(Database.BatchableContext bc)
    {
        
    }
    
}