0

I keep getting this error when I try to abort a scheduled job in my batch job code.

Below is my code:

Datetime dt = system.now().addMinutes(Integer.valueof(myCustomObject.Time_Interval__c));
String day = string.valueOf(dt.day());
String month = string.valueOf(dt.month());
String hour = string.valueOf(dt.hour());
String minute = string.valueOf(dt.minute());
String second = string.valueOf(dt.second());
String year = string.valueOf(dt.year());  
String strJobName = 'TestJob-' + String.valueof(dt);                        
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;


if(!test.isRunningTest())
{
    System.schedule(strJobName, strSchedule, new VGA_scheduleManualLeadAssignment(objplc.Id));
}
List<CronTrigger> lstCron = [select Id from CronTrigger where NextFireTime = Null AND State = 'DELETED'];                        
 if(lstCron != Null && !lstCron.isEmpty())
 {
                    for(CronTrigger obj : lstCron) 
                    {

                      if(obj.Id != Null)
                      {

                          system.abortjob(obj.Id); 
                          //Errors out sometimes //
                      }
                    }
 }

This error is thrown sometimes ---> Job does not exist or is already aborted.

3
  • 1
    Related: Why Deleting Scheduled Jobs with Apex Suddenly Stopped Working March 2015. A DELETED CronTrigger is already considered an aborted job. Why are you trying to abort it again? Commented Oct 2, 2018 at 1:16
  • I want them to be removed from list of scheduled jobs as my batch is running every 1 min and it creates a big list of already executed job
    – Ramy
    Commented Oct 2, 2018 at 1:38
  • why not at the end of the schedulable's execute(), abort the existing job and schedule a new one?
    – cropredy
    Commented Oct 2, 2018 at 2:05

1 Answer 1

0

As per the salesforce documentation :

System.abortJob(jobId) Stops the specified job. The stopped job is still visible in the job queue in the Salesforce user interface.

Here you are trying to abort 'DELETED' jobs. The System.abortJob method takes a jobId as a parameter. If jobId that is no longer valid then you will get above error when attempting to abort it.

5
  • I have added a check if(obj.Id != Null) - To make sure jobid exists. Still it throws error
    – Ramy
    Commented Oct 2, 2018 at 1:32
  • Yes, jobId is not null, but it is not a valid id now, as the record(job) is deleted. Commented Oct 2, 2018 at 1:36
  • Could you please suggest how to remove the already executed job using Apex? I want them to be removed from list of scheduled jobs as my batch is running every 1 min and it creates a big list of already executed job
    – Ramy
    Commented Oct 2, 2018 at 1:39
  • I think you can use Database.emptyRecycleBin(recordIds) method here to delete these jobs. Commented Oct 2, 2018 at 1:47
  • Does this resolve your issue? Commented Oct 7, 2018 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .