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.
DELETED
CronTrigger is already considered an aborted job. Why are you trying to abort it again?execute()
, abort the existing job and schedule a new one?