Bulk Operations In Salesforce Using Batch Apex

Introduction or Scenario

There was a requirement to perform mass or bulk updates on thousands of records for a specific object triggered by certain actions for one of our consulting firm clients based in Atlanta, GA, USA.

Updating records in Apex is a familiar process, but handling operations on a large volume, such as thousands of records, becomes challenging due to Salesforce governor limits, restricting the number of records you can process at a time.

However, for medium to large enterprises, managing thousands of records daily—adding, updating, or deleting them when necessary—is crucial. To address this, Salesforce introduced Batch Apex, a robust concept allowing manipulation of large volumes of records by processing them in smaller batches.

Approach: To update thousands of records efficiently, we needed to develop a Batch Apex class capable of mass updating records for the specific object. Batch Apex operates by breaking down the record set into manageable chunks of data, handling processing across the entire set in smaller batches.

Process: We’ve developed a custom global Apex class that extends the Database.Batchable interface, informing the Salesforce compiler that this class contains batch jobs. Below is a sample class designed to update all records for the Account object (for instance, assuming your organization has over 50 thousand records and requires a mass update for all).

Batch Class

global class batchclass 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, Status FROM Account';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Account> accountList) {
        List<Account> accountListUpdate = new List<Account>();
        // process each batch of records default size is 200 
        // We can define the batch size and maximum batch size is 2000.
        for(Account a : accountList) {        
            // Update the Account status 
            a.Status = 'Closed';
        try {
            // Update the Account Record
            update accountListUpdate;
        } catch(Exception e) {
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email

Upon utilizing the code, navigate to the Developer Console, access the “Debug” tab, and proceed to “Open Execute Anonymous Window.” Enter the provided code in the designated box and click “Execute” to observe the result.

// default syntax to execute batch class
batchclass b = new batchclass();

// If we want to define batch size then we can use below syntax to execute batch class
batchclass b = new batchclass();

Summary or Recap

Batch Apex allows the addition, update, or deletion of thousands of records for a specific object. It operates asynchronously, offering a dedicated framework for processing extensive record sets, exhibiting more leniency in governor limits compared to synchronous code.