Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Last active February 9, 2020 18:42
Show Gist options
  • Save dhaniksahni/a15358637775043efd0217b1cebb6679 to your computer and use it in GitHub Desktop.
Save dhaniksahni/a15358637775043efd0217b1cebb6679 to your computer and use it in GitHub Desktop.
Queueable Finalizer
public class EmailHelper {
public boolean sendErrorNotification(string jobId)
{
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'itops@salesforcecodex.com' };
message.subject = 'Error in Job :' +jobId ;
message.htmlbody = 'Hello Admin, </br>There is error in job.</br> Please check</br></br>. Apex Job';
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) {
return true;
} else {
return false;
}
}
}
public class SendEmailFinalizer implements Finalizer {
public void execute(FinalizerContext context){
Id parentQueueableJobId = context.getAsyncApexJobId();
switch on context.getAsyncApexJobResult() {
when SUCCESS {
System.debug('Job ID: ' + parentQueueableJobId + ' Succeeded');
}
when UNHANDLED_EXCEPTION {
//Send notification to IT Ops Team
EmailHelper.sendErrorNotification(parentQueueableJobId);
System.Debug('OH NOES! (Job ID: ' + parentQueueableJobId +'):FAILED! with error: ' + context.getAsyncApexJobException().getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment