-2

I was learning batch apex and it has three method which is start,execute and finish. Each method has a parameter Database.BatchableContext.

What is this parameter and why it is being used in all three methods?

  • 4
    did you look in the doc ? Please elaborate using edit after reading the doc – cropredy Sep 18 at 17:35
2

A common use case for this is in the finish() method to see if any of the execute transactions had errors

public void finish(BatchableContext bc) {

  AsyncApexJob[] jobs = [SELECT Id, NumberOfErrors 
    FROM AsyncApexJob 
    WHERE Id = :bc.getJobId()];

  if (jobs[0].NumberOfErrors > 0) {..do something, perhaps log/message}
}

For start(), you might want to abort the job if you detect another batchable already running.

4

Personally, I've never used that parameter. But it is described in BatchableContext Interface as:

BatchableContext Interface

Represents the parameter type of a batch job method and contains the batch job ID.

Useful I guess if you want to manipulate the job that runs the batchable e.g. to abort it via System.abortJob.

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.