+ Start a Discussion
BI AdminBI Admin 

Scheduled job only executing the class in full the second time it is called

Hello all,

We are having a problem with a scheduled job that we scheduled to execute every day at 23.00 and at 23.15.

The first run is not fully executed for some reason, but the same job completes without errors 15 minutes later. This happens every day. If we reschedule both jobs, then they are both executed on the first day, but on the second day we have the same situation that the first one is not fully executed.

We checked the logs and they differ starting from this row:

Class.CleaningTasks.execute: line 26, column 1: [SELECT Id,Name FROM Attachment WHERE Name LIKE 'image%.png%']: executed 0 times in 0 ms

We do not understand why this SOQL Statement is not executed in the first run. There is no error thrown and it shows as completed in the job overview.

I couldn't find any examples on the web where something was "executed 0 times" in Salesforce.

In the log of the second run we can see all the limits (in the first log this part is also missing) and we are not close to hitting any limits so this should also not be the problem.

Any help would be appreciated as Salesforce Support was unfortunately not very helpful and just advised us to upgrade to Premium Support.

Thanks!
Andy on CloudAndy on Cloud
Hi,

First I would do is to make sure first job is not running because of second job, sometimes we have that kind of illusion that it seems related but I'd like to be 100%, if you do not have a sandbox to test, I recommend you delete the second job at 23:15, run it manually.  And see if job at 23:00 runs successfully.

Second, I would look at the start method by running same query in developer console's query editor (make sure you use limit in case it queries too much data).  A batch does not do anything if query returns 0 in the first place so I would check that to see if it returns any data.

Third, I would check if there is any condition that is blocking the run in execute method but that goes beyonds this because you cannot share the code and I cannot tell.

Good luck,

Andy
BI AdminBI Admin
Hi Andy, 

Thanks a lot for your reply.

You are right, if we delete the second job, the first job does not complete.
If we delete the first job, the second job does not complete.

Actually, the first job never completes and only the second time the job is called it completes.

Checking it in the devlopment console, the query executes, but I am getting a

Communication failure: "No response from server." - occassionally.

I don't think there is anything blocking it, I can share the code here, no problem. All it is supposed to do is delete Image attachments:
 
global class CleaningTasks implements Schedulable {
  
global string errMessage = '';
    
  global void execute(SchedulableContext SC) {
     
          Integer iJobs = [Select Id From AsyncApexJob where Status = 'Processing'].size();
        if (iJobs >= 5 || Test.isRunningTest()) {
            //If number of batch jobs exceeds limit, send notification email
            Messaging.Singleemailmessage msg = new Messaging.Singleemailmessage();
            msg.setSubject('Job CleaningTasks (' + System.now().format() + ') Failed');
            msg.setPlainTextBody('Too many jobs');
            msg.setToAddresses(new String[] {'xxx@xxxx.com'});
            msg.setBCCAddresses(new String[] {'xxxx@xxxx.com'});
            Messaging.sendEmail(new List<Messaging.Singleemailmessage>{msg});
        }   
           
      
      List<Attachment> ToDelete;
      try {  

      ToDelete = [SELECT Id,Name FROM Attachment WHERE Name LIKE 'image%.png'];
      
        delete ToDelete;
             
               
        } catch (Exception e) {
            errMessage += '<br/>' + e.getMessage();
        }
        
        
                Messaging.Singleemailmessage msg;
             String sSubject;
            msg = new Messaging.Singleemailmessage();
            sSubject = 'Results from CleaningTasks (' + System.now().format() + ')';
            msg.setSubject(sSubject);
            msg.setToAddresses(new String[] {'xxx@xxxx.com'});
            msg.setBCCAddresses(new String[] {'xxxx@xxxx.com'});
                  msg.setPlainTextBody('errMessage: ' + errMessage+'\r\n'+
                                'Image attachments deleted: '+ ToDelete.size()+'\r\n'
                                
                                );
      
            Messaging.sendEmail(new List<Messaging.Singleemailmessage>{msg});
       
    }
    
}