Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Last active January 11, 2025 16:01
Show Gist options
  • Save msrivastav13/6291713 to your computer and use it in GitHub Desktop.
Save msrivastav13/6291713 to your computer and use it in GitHub Desktop.
Sending Email Notification In finish Method
global void finish(Database.BatchableContext BC) {
// Get the ID of the AsyncApexJob representing this batch job
// from Database.BatchableContext.
// Query the AsyncApexJob object to retrieve the current job's information.
AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
from AsyncApexJob where Id =:BC.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Match Merge Batch ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
@eyebe-git
Copy link

This is exactly what I was looking for, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment