Activity Forums Salesforce® Discussions How to query and abort scheduled job using Apex in Salesforce?

  • Nikita

    Member
    August 23, 2019 at 6:00 am

    Hi Prachi,

    To abort a scheduled job went to Setup > Jobs > Apex Jobs or Setup > Jobs > Scheduled Jobs There was Abort option 🙂

  • Deepak

    Member
    August 23, 2019 at 9:35 am

    I have a schedule class named schedGroupAccounts and I scheduled it for every 15 minutes via the execute anonymous in the developer console.

    Code Snippet for Scheduling apex class
    System.schedule('Scheduled Job 1','0 15 * * * ?',new schedGroupAccounts());
    To monitor your apex jobs, go to Setup -> Monitor -> Jobs -> Apex Jobs.

    So you scheduled it, now you want to abort the job for some reason. How can you do? The jobs we schedule will be available in the crontrigger object. Lets query the trigger and see how we can retrieve the particular job to abort it.
    SELECT CreatedById, CreatedDate, CronExpression, CronJobDetailId, EndTime,Id, LastModifiedById, NextFireTime, OwnerId, PreviousFireTime, StartTime, State, TimesTriggered FROM CronTrigger
    So from the above query, I found my job based on the cronexpression and createddate. I want to abort that job. So I get the id of the job and assign it to the soql query.

    CronTrigger ct = [SELECT Id FROM CronTrigger where Id=''];
    System.abortJob(ct.Id);

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos