Last active
February 9, 2020 18:42
-
-
Save dhaniksahni/a15358637775043efd0217b1cebb6679 to your computer and use it in GitHub Desktop.
Queueable Finalizer
This file contains 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
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; | |
} | |
} | |
} |
This file contains 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
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