You need to sign in to do that
Don't have an account?

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
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
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...
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.
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.From Winter 13 this is possible
Have a look at this https://success.salesforce.com/ideaView?id=08730000000KNzxAAG