function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Send email notification when schedule class failes

I have schedule my class and i want to send the notification when schedule job failes. Here is the code which i have implemented:

global class CaseAssignmentScheduler00 implements Schedulable  
    public static dateTime;
    public static String Csec=String.valueof(dt.second());
    public static String Cmin=String.valueof(dt.minute());
    public static String Chr=String.valueof(dt.hour());
    public static String Cday=String.valueof(;
    public static String Cmonth=String.valueof(dt.month());
    public static String CYear=String.valueof(dt.Year());
    public static String SchTimer= Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
    list<string> emailaddress = new list<string>(); 
    public static integer count=0;
    global void execute(SchedulableContext SC)     
        catch(exception e){
    public void doScheduling(){
        CaseAssignmentScheduler00 cas = new CaseAssignmentScheduler00();
        system.schedule('CaseAssignmentScheduler00: Assigning Cases from Queue to Active Users at ', SchTimer, cas);
            for( CronTrigger c:[Select c.TimesTriggered, c.TimeZoneSidKey, c.State, c.StartTime, c.PreviousFireTime, c.OwnerId, c.NextFireTime, c.LastModifiedById, c.Id, c.EndTime, c.CronExpression, c.CreatedDate, c.CreatedById From CronTrigger c where c.NextFireTime=null  AND c.State='DELETED' Limit 100]){
    /****** Send email notification to Admin if Job fails*****/
    public void checkJobStatus(){
        // Featch the Email ID from custom setting
        for(Case_Scheduler_Email__c Em : Case_Scheduler_Email__c.getAll().values()){
        for(AsyncApexJob aj: [Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, ExtendedStatus, createdDate ,ApexClassID, MethodName
                from AsyncApexJob where Status = 'Failed' LIMIT 1])
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        mail.setSenderDisplayName('Case Scheduler Job');
                        mail.setSubject('Case Scheduler Job Fails');
                        mail.setHtmlBody('The Scheduled batch Apex job got failed. Please check the schedule job and take the necessary action. ' +'Here is the Error Message: ' + aj.ExtendedStatus);
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Seem this code is not working. can any one help me on this ?

It seems to me that you are facing indefinite recurrence, because you are scheduling your job inside execute method.

So, let say when you schedule your class through anonymous block, it will call execute method, which again schedule the same class and it will go on and on.....................