function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdeveloper9sfdeveloper9 

Running Batch Apex classes in sequence`

I have to run few batch apex classes in a sequence. For example I have BatchApex1, BatchApex2 and BatchApex3. I want BatchApex2 to be started only after BatchApex1 is complete.

similarly BatchApex3 should start only after BatchApex2 is complete. please suggest me how can I achieve this.

 

Thanks in advance.

RS

Message Edited by sfdeveloper9 on 12-19-2009 02:23 PM
Best Answer chosen by Admin (Salesforce Developers) 
sfdeveloper9sfdeveloper9

yes, you are right that won't work because you cannot call a Batch Apex from a finish method of another Batch Apex.

I was able to figure out a work around by creating a Email Handler class which would parse the the email body sent in the finish method of the first Batch Apex and call the subsequent Batch Apex.

All Answers

XactiumBenXactiumBen

You could attempt to create a custom object that stores which batches are complete then use a trigger on this object to start up the batches in order.

 

Something like:

 

1. Initially Run BatchApex1.

2. When BatchApex1 is complete create a record in new custom object and flag as complete

3. In new custom object trigger - fire BatchApex2 if BatchApex1 record == complete

4. When BatchApex2 is complete create a record in new custom object and flag as complete

etc...

luckymeluckyme

I don't think this would work. It cannot really overcome the platform limitation that you cannot start a batch within another one. When the trigger tries to start the next batch, you are still executing the previous batch. And you would get an error if you try to start another batch. There is no fundamental difference between this approach and try to kick off the next batch directly in the finish() method.

sfdeveloper9sfdeveloper9

yes, you are right that won't work because you cannot call a Batch Apex from a finish method of another Batch Apex.

I was able to figure out a work around by creating a Email Handler class which would parse the the email body sent in the finish method of the first Batch Apex and call the subsequent Batch Apex.
This was selected as the best answer
geetha charangeetha charan
you can call the next batch apex in the finish method 

From Winter 13 this is possible

Have a look at this https://success.salesforce.com/ideaView?id=08730000000KNzxAAG