+ Start a Discussion
Saswat MohantySaswat Mohanty 

email notification on batch job failure

Hello,

Currently in our org there are many batch jobs which run and perform certain activities. Now the requirement is to notify the admin immediately via email when any of the batch jobs is failed . Though I know, there is a mail template which can be implemented in finish method of each batch job, however it is difficult to put in every batch jobs as it is time consuming.

Could you please help in getting a common class which will check what are the batch jobs that finish the run for a particular day(ideally every day) and then for failed jobs it will send immediate email notification?
KaranrajKaranraj
Hi Saswat - Sending email notification in the finish method is the best way to get immediatenotification when batch jobs failed else you may have to endup in developing one more scheduled batch class to get failed job details and send you in email and this approach won't send immediately, you have to scheduled that to run in a certain time interval.

To make your implementation simple, develop a utility class which will send email notification about the job detail and call that utility class in your finish method but yes you have to edit all batch class.

Thanks,
Karan
Mohit Bansal6Mohit Bansal6
Hi Saswat

You can create a new batch class and add below code to it to get email notification for failed batches:

Below is the reference code. You can modify them as per your needs:
global void execute(Database.batchablecontext bc) {

    // Check batch status - IF COMPLETED then 
    AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
        TotalJobItems, CreatedBy.Email, ExtendedStatus
        from AsyncApexJob where Status == 'Failed'];        

    if(a.Status == 'Failed') {


        // 1. Send Email (CSV created in execute method)


        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // Send the email to the job submitter
        String[] toAddresses = new String[] {a.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('x70RecordExtract  Status: ' + a.Status);
            mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
            ' batches with '+ a.NumberOfErrors + ' failures. ExtendedStatus: ' + a.ExtendedStatus);

        // Add your attachment to the email.

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


        // 2. database.executebatch(new chain_batch(),200);

    }
}


In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Saswat MohantySaswat Mohanty
@ Mohit: Ya that is fine which can be achieved in the finish method and I also do not have any problem with it. My only concern was in our org there were around more than 50 batch jobs and it is time consuming to put this template. Therefore I was asking if there is any other method or single class from where the immediate email notification will be generated for each batch job if they are failed.
Saswat MohantySaswat Mohanty
@Karan : Yes in our org there is a util class which is being called in most of the batch classes. Could you just give hint how to achieve that?
May be in a method I should extract job ids from Asyncapex job and then for failure ones only will send email?
Mohit Bansal6Mohit Bansal6
Hi Saswat We have to a create a separate batch class which will check the all BatchJobs status to check if any batch job status is failed. BatchJob is salesforce standard object which track the status of each batch ran. You can schedule this batch to run after every 2 hours or any number as per need, which will shot you email for batch status which ran in last two hours or a whole day. I hope this helps in implementing your functionality. Regards Mohit Bansal