Batch Apex Example In Salesforce

Batch Apex Example In Salesforce

Batch Apex Example In Salesforce

What is Batch Apex in Salesforce?

Batch class in salesforce is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits. If you have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably your best solution.

Batch Apex Example In Salesforce

Here’s how Batch Apex works under the hood. Let’s say you want to process 1 million records using Batch Apex. The execution logic of the batch class is called once for each batch of records you are processing. Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction.

Advantage of using batch Apex

  • Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
  • If one batch fails to process successfully, all other successful batch transactions aren’t rolled back


Batch Apex Syntax

To write a Batch Apex class, your class must implement the Database.Batchable interface and include the following three methods:

Start method is automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These records are divided into subtasks and pass those to execute method.

Used to collect the records or objects to be passed to the interface method execute for processing. This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.

Most of the time a QueryLocator does the trick with a simple SOQL query to generate the scope of objects in the batch job. But if you need to do something crazy like loop through the results of an API call or pre-process records before being passed to the execute method, you might want to check out the Custom Iterators link in the Resources section.

With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

Execute Method performs an operation which we want to perform on the records fetched from start method.

Performs the actual processing for each chunk or “batch” of data passed to the method. The default batch size is 200 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

This method takes the following:

  • A reference to the Database.BatchableContext object.
  • A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.

Finish method executes after all batches are processed. Use this method to send confirmation email notifications

Used to execute post-processing operations (for example, sending an email) and is called once after all batches are processed.

Here’s what the skeleton of a Batch Apex class looks like:

global class MyBatchClass implements Database.Batchable<sObject> {
    global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
    global void execute(Database.BatchableContext bc, List<P> records){
        // process each batch of records
    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations

Key points about batch Apex

  • To use batch Apex, write an Apex class that implements Database.Batchable interface and make your class global
  • Implement the following 3 methods
      • start()
      • execute()
      • finish()
  • The default batch size is 200


Monitoring Batch Apex

To monitor or stop the execution of the batch Apex job, from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.

Batch class in salesforce

Here is example of Batch class in salesforce

global class BatchApexExample implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        String query = 'SELECT Id, Name FROM Account';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        // process each batch of records default size is 200
        for(Account acc : accList) {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'sfdcpoint';
        try {
        	// Update the Account Record
            update accList;
        } catch(Exception e) {
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations like sending email

Invoking a Batch Class

To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:

MyBatchClass myBatchObject = new MyBatchClass(); 
Id batchId = Database.executeBatch(myBatchObject);

You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch. Pro tip: you might want to limit this batch size if you are running into governor limits.

Id batchId = Database.executeBatch(myBatchObject, 100);

Each batch Apex invocation creates an AsyncApexJob record so that you can track the job’s progress. You can view the progress via SOQL or manage your job in the Apex Job Queue. We’ll talk about the Job Queue shortly.

AsyncApexJob job = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID = :batchId ];


Scheduling Batch Apex

can also use the Schedulable interface with batch Apex classes. The following example implements the Schedulable interface for a batch Apex class called batchable:

global class scheduledBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
      BatchApexExample b = new BatchApexExample(); 


Test class for batch Apex

private class BatchApexExampleTest {
    static testmethod void test() {
        // Create test accounts to be updated by batch
	Account[] accList = new List();
	for (Integer i=0;i<400;i++) {
	    Account ac = new Account(Name = 'Account ' + i);
	insert accList;

            BatchApexExample b = new BatchApexExample();
        // Verify accounts updated
	Account[] accUpdatedList = [SELECT Id, Name FROM Account];

Best Practices for batch Apex

As with future methods, there are a few things you want to keep in mind when using Batch Apex. To ensure fast execution of batch jobs, minimize Web service callout times and tune queries used in your batch Apex code. The longer the batch job executes, the more likely other queued jobs are delayed when many jobs are in the queue. Best practices include:

  • Only use Batch Apex if you have more than one batch of records. If you don’t have enough records to run more than one batch, you are probably better off using Queueable Apex.
  • Tune any SOQL query to gather the records to execute as quickly as possible.
  • Minimize the number of asynchronous requests created to minimize the chance of delays.
  • Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger won’t add more batch jobs than the limit.


For more details about batch apex refer to batch apex trailhead

Permanent link to this article:


    • Alexis Pérez on March 20, 2020 at 9:02 pm
    • Reply

    Is it possible to calculate averages within a batch and that in each block that is processed can this information be preserved to obtain a total calculation of all the records?

    I need to calculate averages per month, by municipality, state and country of many records, but this question arises

    I look forward to your comments, greetings

    1. Yes, Its possible to preserve information across different batches created from single batch run. You need to implement Database.Stateful class for that in your batch class
      Here is more detail about it

Leave a Reply

Your email address will not be published.