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
AgPrernaAgPrerna 

Batch Apex to send Email notification on Email ID change

Hi,
My requirement is as follows:
- Account is the parent of Digital App object.
-One Account can have multiple Digital Apps.
- If email address is changed in any Digital App, run the batch after 30 mins delay clubbing all the email address change for a particular account.
- within this 30 mins whatever email address is changing we will send the notification.
- If any change is made after 30 mins, it will be part of next batch run.

What i have done till now is:
- Created a new field in Account to set the 30 min delay
- new field in Digital App to store old email address (email notificatioon should be triggered to both changed and old email address)
-Batch Apex:
global class DigitalAppLoginChange implements Database.Batchable <sObject>{
    global Database.QueryLocator Start(Database.BatchableContext bc){
        DateTime currentTime = System.now();
        String query= 'Select id, JJ_Email_Updated__c,JJ_App_User_Old_Email__c,JJ_Linked_Account__c,JJ_AppUser_Login_Email__c, JJ_Last_Email_Changed_Time__c from JJ_Digital_App__c where LastModifiedDate=TODAY AND JJ_Linked_Account__c != null AND JJ_Linked_Account__r.JJ_Digital_App_Time_Delay__c <=: currentTime ';
        return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext bc, List<JJ_Digital_App__c> records){
         List<String> emails2 = new List<String>();
         Set<id>Accountid = new Set<id>();
         Map<id,String> emailAcc = new Map<id,String> ();
        System.debug('Test digital app ids'+records);
        for(JJ_Digital_App__c dApp :records){
            if((dApp.JJ_App_User_Old_Email__c != dApp.JJ_AppUser_Login_Email__c) && dApp.JJ_Email_Updated__c == True){
                emailAcc.put(dApp.JJ_Linked_Account__c,dApp.JJ_AppUser_Login_Email__c);
                Accountid.add(dApp.JJ_Linked_Account__c);
                System.debug('Test digital app ids'+dApp.id);
                dApp.JJ_Mobile_Updated__c=FALSE;
                update dApp;
                
            }
            //dApp.JJ_Mobile_Updated__c=FALSE;
                
            }
            for(Id acckey:emailAcc.keyset()){
            emails2.add(emailAcc.get(acckey));
            System.debug('List of email changes2'+emails2);
            System.debug('List of Acc email changes'+emailAcc);
            System.debug('List of affected Accounts'+Accountid);
                
           
}
global void finish(Database.BatchableContext bc){
}
}

How to procced?
TIA