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
praiprai 

Send email notification when schedule class failes

Hi,
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 dt=System.now().addMinutes(5);
    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(dt.day());
    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)     
    {
       
        try{
            CaseRoundRobinAssignmentController.CaseAssignmentFromQueue();
            doScheduling();
            
        }
        catch(exception e){
    }
    
    public void doScheduling(){
        system.debug('*************SchTimer:'+SchTimer);
        CaseAssignmentScheduler00 cas = new CaseAssignmentScheduler00();
        system.schedule('CaseAssignmentScheduler00: Assigning Cases from Queue to Active Users at '+System.now().format(), SchTimer, cas);
        //checkJobStatus();     
            
            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]){
                System.abortJob(c.id);
            }
    }
    
    /****** 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()){
                             emailaddress.add(Em.Email__c);      
        }
        
        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.setToAddresses(emailaddress);
                        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 ?

 
JethaJetha
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.....................