blog image
Blog Articles

Comprehensive Guide to Batch Apex Classes in Salesforce


Within the dynamic landscape of Salesforce development, enhancing the processing of substantial datasets is imperative for achieving peak efficiency and performance. This blog navigates the influential realm of Batch Apex Classes, unveiling their transformative prowess, functionalities, benefits, and best practices. Guiding developers through a journey of optimization empowers them to unlock the full potential of Salesforce. As we focus on the intricacies of Batch Apex, we highlight its pivotal role in streamlining operations, ensuring scalability, and adhering to best practices. This exploration serves as a beacon for developers, providing insights that elevate their proficiency in harnessing the capabilities of Batch Apex Classes for unparalleled success in Salesforce development.

Understanding Batch Apex

Syntax Explanation
  • global class MyBatchClass – Defines a global Apex class named MyBatchClass.
  • implements Database.Batchable<sObject> – Specifies that the class implements the Database.Batchable interface for processing Salesforce records.
  • start method – Initializes and collects records to be processed. It returns a Database.QueryLocator to define the scope of records.
  • execute method – Processes each batch of records. Your logic for record processing goes here.
  • finish method – Performs any post-processing logic after all batches are processed.

To execute this batch class, you would initiate it using the Database.executeBatch method. For instance:

Batch Functions

In Salesforce Batch Apex, the start, execute, and finish methods play distinct roles in orchestrating the processing of records. Let’s delve into the functionalities of each function:

Start Method:

  • Functionality: Initializes and collects the records that will be processed in batches.
  • Usage:
    • Define the scope of records to be processed using a Database.QueryLocator object.
    • Typically involves constructing a SOQL query to identify records based on certain criteria.
  • Example:

Execute Method:

  • Functionality: Processes each batch of records returned by the start method.
  • Usage:
    • Logic within this method is applied to each record in the batch.
    • The primary area for performing data manipulation, calculations, or other operations.
    • Any DML (Data Manipulation Language) operations should be executed in this method.
  • Example:

Finish Method:

  • Functionality: Executes after all batches have been processed by the execute method.
  • Usage:
    • Typically used for any post-processing or cleanup logic.
    • Can be employed for tasks like sending notifications, logging results, or updating status fields.
  • Example:

Governor Limits

Salesforce imposes governor limits on Batch Apex to ensure efficient resource utilization and prevent misuse that could adversely impact the platform’s performance.

The governor limits associated with Batch Apex include:
  1. Total Processing Time:
    • Limit: 120 seconds per batch.
    • Description: The cumulative time taken by all batches within a transaction must not exceed this limit.
  2. Heap Size:
    • Limit: 12 MB for synchronous and 6 MB for asynchronous operations.
    • Description: Represents the total memory consumed by the execution of Batch Apex. It includes the size of all variables and data structures.
  3. Number of Records Processed:
    • Limit: 50 million records.
    • Description: The total number of records processed by all batches in a single transaction should not exceed this limit.
  4. Number of Batch Jobs:
    • Limit: 2,000 concurrent or queued batch jobs.
    • Description: The maximum number of Batch Apex jobs that can be in progress or queued for execution at a given time.
  5. Maximum Query Rows:
    • Limit: 50,000 rows.
    • Description: The number of records retrieved in a query should not exceed this limit.
  6. Maximum Query Locator Rows:
    • Limit: 50 million rows.
    • Description: The number of rows returned by a Database.QueryLocator in the start method should not exceed this limit.
  7. Number of DML Statements:
    • Limit: 150 DML statements.
    • Description: The total number of Data Manipulation Language (DML) operations (insert, update, delete) allowed in a transaction.
  8. Number of Database Methods Invoked:
    • Limit: 250,000 database method calls.
    • Description: The total number of methods that use DML statements, such as insert, update, upsert, delete, undelete, or merge.
  9. Callouts:
    • Limit: 100 callouts per batch.
    • Description: The number of HTTP callouts a batch job can make.

It’s crucial for developers to be mindful of these governor limits while designing and executing Batch Apex jobs to ensure compliance and maintain the performance and stability of the Salesforce platform. Additionally, Salesforce regularly updates its limits, so it’s advisable to refer to the latest Salesforce documentation for the most up-to-date information.


In conclusion, the start, execute, and finish methods in Salesforce Batch Apex collectively form a robust and organized framework for handling large datasets. The start method initiates the process by defining the scope of records, the execute method performs batch-specific operations on these records, and the finish method allows for post-processing tasks. This triad of functions ensures not only the efficient processing of extensive data volumes but also provides developers with the flexibility to customize and optimize their data operations.

By breaking down large datasets into manageable chunks, Batch Apex enhances system performance and responsiveness, adhering to Salesforce governor limits. Developers can leverage the functionalities of these methods to implement complex business logic, handle errors gracefully, and perform any necessary cleanup tasks. The versatility of Batch Apex, coupled with the strategic use of its methods, empowers developers to build scalable and efficient solutions, thereby enhancing the overall performance and responsiveness of Salesforce applications.

Sign Up Now
Get a Fast Estimate on Your Software Development Project

We are committed to delivering high-quality IT solutions tailored to meet the unique needs of our clients. As part of our commitment to transparency and excellence, we provide detailed project estimations to help our clients understand the scope, timeline, and budget associated with their IT initiatives.