0
private void reschedule(CronTrigger cronTrigger, ISchedulable schedulable, MySchedulableContext context) {
        System.abortJob(cronTrigger.Id);
        String newTrigger = schedulable.getNextTriggerString(context);
            System.schedule(cronTrigger.CronJobDetail.Name, newTrigger, this);
        
    }

In the code above, I kept getting job already exists or is aborted on the line:

System.abortJob(cronTrigger.Id);

Looking at a couple of questions here:

Apex CronTrigger Throws error while aborting: Job does not exist or is already aborted.

Why Deleting Scheduled Jobs with Apex Suddenly Stopped Working March 2015

So I did:

private void reschedule(CronTrigger cronTrigger, ISchedulable schedulable, MySchedulableContext context) {
        if(cronTrigger.Id != Null && cronTrigger.State != 'ERROR' && cronTrigger.State != 'DELETED' ){
        System.abortJob(cronTrigger.Id);
}
        String newTrigger = schedulable.getNextTriggerString(context);
            System.schedule(cronTrigger.CronJobDetail.Name, newTrigger, this);
        
    }

Are there any other states that I should worry about ? For example the Cron Trigger State can also be COMPLETE amongst others and will aborting a COMPLETE job give the same error?

1 Answer 1

2

This is one of those rare cases where I'd probably say: just try-catch and ignore any exceptions. If it fails to abort, it should be harmless, so I see no reason to worry about error handling here.

private void reschedule(CronTrigger me, ISchedulable item, MySchedulableContext context) {
    try {
        System.abortJob(me.Id);
    } catch(Exception e) {
        System.debug('Could not abort (harmless)...');
    }
}

You must log in to answer this question.

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