Tuesday, January 17, 2017

How to query and abort scheduled job using Apex.

While updating class on Sandboxes, chances are high that it is being used in any scheduler, so below code will abort all scheduled job. If there are 150+ scheduled job, then you might want to use below code again and again in developer console until all jobs are removed from system.


//Limit is 150 because System.abortJob counted against DML 
List<CronTrigger> abort_job = [SELECT Id FROM CronTrigger 
                               WHERE State != 'Deleted' limit 150];
    for (CronTrigger t : abort_job) { //for each record
//try catch - to make sure one fail should not impact other jobs which needs to be cancelled
     try{
        System.abortJob(t.Id); //abort the job
     }catch(Exception e){}
     
    }

No comments:

Post a Comment