Friday, 18 November 2011

Serialize Batch Apex

In you cannot call a batch Apex class from another batch Apex class because batch Apex is a future call. However, you can use Database.Stateful and the finish() method to mimic serialization of batch processes.

After batch 1 is complete in the finish() method this calls startNewBatch() in GeneralUtils which fires the newbacth batch class

However, running this you will see an error
Database.executeBatch cannot be called from a batch or future method.

global class batch l implements Database.Batchable<sObject>, Database.Stateful{

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC,
List<sObject> scope){


global void finish(Database.BatchableContext BC){



public class GeneralUtils{

 public static void startNewBatch(){
    newbacth batchable = new newbacth();
newbacthID = Database.executeBatch(batchable);


  1. Hi Steve,
    Interesting problem. Did you ever encounter a decent solution? I was thinking of using time-based workflow to handle the batch-to-batch call..

  2. In the last release they were talking about allowing batches calling batches


Note: only a member of this blog may post a comment.