Back to Top

Batch Apex Example In Salesforce

Updated 6 February 2026

Batch Apex in Salesforce is a necessary survival skill for every Salesforce developer. 

It exists in Salesforce for one reason: to process massive data volumes safely inside Salesforce. 

Instead of trying to process everything in a single transaction, the platform breaks the workload into manageable, independent chunks.

Let’s dive into the topic and understand what Batch Apex actually does under the hood.

What is Batch Apex in Salesforce?

Batch Apex is an asynchronous Apex framework for processing large record sets in manageable transactions. Each batch runs in its own transaction with fresh limits.

Searching for an experienced
Salesforce Company ?
Find out More

Technically speaking, a Batch Apex class implements: Database.Batchable<SObject> and contains three lifecycle methods:

  • start() → defines the data scope
  • execute() → processes each batch
  • finish() → post-processing logic

Remember, if your code accesses external objects and is used in batch Apex, use Iterable<sObject> instead of Database.QueryLocator.

And the default batch size is 200 records.

Why does this matter?

Standard synchronous Apex runs in a single execution context. The problem with them is that when limits are hit, the entire transaction fails.

On the other hand, Batch Apex avoids this by distributing the workload across multiple transactions.

This makes it ideal for:

  • Data cleanup jobs
  • Migration tasks
  • Recalculation processes
  • Nightly automation

Apex Batch Class

Create an apex class that implements Database.Batchable interface and class must be global, as mentioned below.

global class batchExample implements Database.Batchable<sObject> {}

Now describe all batchable class method i.e.

 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
}

Updating Account Records with Batch Apex

Here’s a practical example: updating Account records in bulk.

Here is the Batch class to update the account name.

global class batchExample implements Database.Batchable<sObject> {
    /**
        * Webkul Software.
        *
        * @category  Webkul
        * @author    Webkul
        * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
		* @license   https://store.webkul.com/license.html
	*/
    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

        
        for(Account acc : accList)
        {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'Webkul';
        }
        try {
        	// Update the Account Record
            update accList;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Run the BATCHABLE Class in Developer Console

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

batchExample be = new batchExample();
database.executeBatch(be);

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.

batchExample be = new batchExample();
database.executeBatch(be,100);

Account Name after executing batch class:

salesforce

Support

  • Have questions about Batch Apex or advanced Salesforce architecture? Contact our team at [email protected]
  • We also help enterprises design resilient Salesforce architectures built for scale. Hire Salesforce developers from Webkul.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


14 comments

  • BradQ
    • Nishi Kaushik (Moderator)
  • Naveen singh rajput
  • Vinay Chowdary Manduva
    • Nishi Kaushik (Moderator)
  • pritikumari
    • Nishi Kaushik (Moderator)
  • Chandranna
    • Nishi Kaushik (Moderator)
  • Nishi Kaushik (Moderator)
  • Divya Gupta
    • Mahesh
      • Nishi Kaushik (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home