1

I would like to schedule a job to run once in 15 minutes time.

According to this article:

I need to add some code to the finish method:

public void finish(Database.BatchableContext context) {

    AsyncApexJob a = [SELECT Id FROM AsyncApexJob WHERE Id = :context.getJobId()];

    System.abortJob(a.id);
}

It seems to me that the SOQL query is not required and that I could just run this line:

System.abortJob(context.getJobId());

Is that true? or am I missing something?

The documentation for method abortJob says:

The jobId is the ID associated with either AsyncApexJob or CronTrigger.

1

You can actually use what you have mentioned:

System.abortJob(context.getJobId());

The other KB article Run Scheduled Apex job once may have overdone things there. But getJobId() necessarily returns the AsyncApexJob's Id which can be used to abort the job. Below is an excerpt from Using Batch Apex as reference (emphasis mine).

Returns the ID of the AsyncApexJob object associated with this batch job as a string. Use this method to track the progress of records in the batch job. You can also use this ID with the System.abortJob method.

| improve this answer | |
  • 1
    Note that abortion can only be called by a user with the appropriate permission (salesforce.stackexchange.com/questions/9013/…) which basically means a sys admin. So don't try to use this in code that can be run by other types of user. – Phil W Jun 25 '19 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.