Batch Apex Chaining

Learn All About Batch Apex Chaining in Salesforce

Batch apex enables the user to do a single job by breaking it into multiple chunks which are processed separately. Batch apex is useful when we have to process a very large number of records. It is run asynchronously in Salesforce within the limits of the platform which adds to its usefulness. By using batch apex we can follow governor limits and the job is processed when the resource is free without interfering with other processes running at the same time. The failed record in the batches can be rolled back. Similarly, batch chaining refers to the process in which one batch is calling another batch class then both are processing simultaneously. Batch chaining is not restricted to only two batch classes but also the second batch class that is called by the first-class may further call the third batch class, this also comes under batch chaining. 

We can understand this by an example:

Advertisement

First batch class

global class firstBatch implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        //Query
    }
    global void execute(Database.BatchableContext BC, list<sObject> scope){
        //Logic
    }
    global void finish(Database.BatchableContext BC) {
        // Add the logic for calling batch in finish method
        secondBatch obj = new secondBatch (); 
        database.executebatch(obj );
    }
}

dont miss out iconDon't forget to check out: Salesforce Connect - Quick Start | Apex Developer Guide

Understanding the above code:

  1. start() method is automatically called at the very beginning of the execution, basically, it collects all the records on which the operation has to be performed.
  2. execute() method performs the logic which is defined on the collection of records that are fetched in the above method.
  3. finish() method is executed after all the batches are finished running.

In the above code, in the finish method, “secondBatch” is being called.

Second batch class:

global class secondBatch implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        //Query
    }
    global void execute(Database.BatchableContext BC, list<sObject> scope){
        //Logic
    }
    global void finish(Database.BatchableContext BC){
    }
}

dont miss out iconCheck out another amazing blog by Anjali here: Defining Reporting Snapshots | Salesforce Guide

There are several ways in which you can schedule the batch class

  1. Firstly, simply schedule it from the UI
  2. Or you can schedule the batch class from the anonymous window.
  3. By using Apex scheduler.

For executing the above batch class from the anonymous window, write the below code:

firstBatch  batchToRun = new firstBatch();
database.executeBatch(batchToRun);

As you schedule the first batch to run, automatically when its finish method will be encountered, the second batch will be scheduled for running.2

Responses

Popular Salesforce Blogs