0

I have three batch Jobs. Batch1, Batch2, Batch3. When the Batch1 starts executing it automatically calls on Batch2 to execute in finish method. Similarly Batch2 calls on Batch3 to execute in the finish method. Once Batch3 finish executing it sends out an email about any records not being processed and the details of the records from all the three batch jobs.

Now I need to schedule these batch jobs in 3 different ways. 1) Manual 2) Nightly 3) Hourly.

For Manual and Nightly schedules the Batch jobs need to be executed the way mentioned above( all three Batches needs to be executed sequentially). But for the hourly schedule only the first 2 (Batch1, Batch2 needs to be executed skipping the Batch3).

Any thoughts on how to schedule this?

1

Use a constructor for your Schedulable class where you can pass in a Boolean isBatch3Enabled.

Use a constructor for each of the three Batchable classes where you can pass in isBatch3Enabled.

Batch1 and Batch2 implements Database.Stateful. This allows the constructor argument to be preserved up through the finish().

Batch1.finish() starts Batch2 via Database.executeBatch(new Batch2(isBatch3Enabled));

Batch2.finish() examines isBatch3Enabled and starts Batch3 if true, otherwise, ends.

You will need to schedule the Schedulable (passing in the argument isBatch3Enabled=false) using anaonymous Apex from the Developer Console as the UI doesn't permit passing values to schedulable class constructors.

| improve this answer | |

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.