Last active
January 11, 2025 16:01
-
-
Save msrivastav13/6291713 to your computer and use it in GitHub Desktop.
Sending Email Notification In finish Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is exactly what I was looking for, thank you.