What is Batch Apex in Salesforce?

In this section, you will understand about Batch Apex in Salesforce in detail. Consider a situation wherein you have to process large amounts of data on a daily basis and have to delete some unused data. It would be difficult to manually do so. This is where batching with Apex Salesforce comes to your rescue.

Watch this informative video on Salesforce from Intellipaat:

Batch Apex in Salesforce

Youtube subscribe

Before going deep into the concept of Batch Apex, let’s see the topics covered in this section:

Batch Apex in Salesforce is specially designed for a large number of records, i.e., here, data would be divided into small batches of records, and then it would be evaluated. In other words, Batch class in Salesforce is specifically designed to process bulk data or records together and have a greater governor limit than the synchronous code.

Advantages of Batch Apex in Salesforce

  • Whenever a transaction is executed, Batch Apex ensures that the code stays within the governor limit.
  • Until a batch is not successfully executed, Batch Apex won’t execute the following batches.
  • A large set of records can be processed together on a regular basis using Batch Apex classes.
  • The interface can be scheduled to run batches at different time periods.
  • Asynchronous operations can be implemented by Batch Apex classes.
  • Batch jobs are invoked programmatically during the runtime and can be operated on any size of records, with a maximum of 200 records per batch. You can break down a larger record data into 200 records per batch to execute it better.

Learn more about Salesforce from this Salesforce Training in New York to get ahead in your career!

Why use Batch Apex in Salesforce instead of the normal Apex?

There are various reasons why Batch Apex is better than normal Apex.

  • SOQL queries: Normal Apex uses 100 records per cycle to execute SOQL queries. Whereas, Batch Apex does the same in 200 records per cycle.
  • Retrieval of SOQL queries: Normal Apex can retrieve 50,000 SOQL queries but, in Batch Apex, 50,000,000 SOQL queries can be retrieved.
  • Heap size: Normal Apex has a heap size of 6 MB; whereas, Batch Apex has a heap size of 12 MB.
  • Errors: When executing bulk records, Normal Apex classes are more vulnerable to encountering errors as compared to Batch Apex.

Get familiar with the top Salesforce Interview Questions to get a head start in your career!

Batch Class in Salesforce

When using a Batch class in Salesforce, the batchable interface needs to be implemented first. It has the following three methods:

  • Start: This method is called at the starting of a batch job to collect the data on which the batch job will be operating. It breaks the data or record into batches. In some cases, the ‘QueryLocator’ method is used to operate with the simple SOQL query to generate the scope of objects inside a batch job.
    • Syntax:
global void execute(Database.BatchableContext BC, list<sobject<) {}
  • Execute: This method executes after the Start method, and it does the actual processing for each batch, separately.
    • Syntax:
global void execute(Database.BatchableContext BC, list<sobject<) {}
  • Finish: This method will be called at last. Since this method is called in the end, it is responsible to perform post-processing operations such as sending an email. When this process is called all batches are already executed.
    • Syntax:
global void finish(Database.BatchableContext BC) {}

Creating an Apex Batch Class in Salesforce with an Example

Step 1: Search for Apex Salesforce Classes in Setup

Creating a Batch Apex with an Example Step 1

Step 2: Once you open Salesforce Apex Classes, click on New to create a new Apex Class

Step 2

Step 3: Below is the screen on which you’ll have to write your code

Step 3

The following is the program that updates the account name in the account object with the keyword ‘Updated’ after the account name.

Step 3 A

// Batch Job for Processing the Records

  global class batchAccountUpdate implements Database.Batchable<sObject> {

// Start Method

    global Database.QueryLocator start(Database.BatchableContext BC) {

        String query = 'SELECT Id,Name FROM Account';

        return Database.getQueryLocator(query);


   // Execute method

    global void execute(Database.BatchableContext BC, List<Account> scope) {

         for(Account a : scope)

         {a.Name = a.Name + 'Updated';            


         update scope;


    // Finish Method

    global void finish(Database.BatchableContext BC) {



Learn new Technologies

Step 4: After writing the code, you’ll have to go to Developer Console and click on Debug and then on Open Execute Anonymous Window. You’ll see the following screen:

Step 4

Enter the following code in the box and click on Execute

batchAccountUpdate b = new batchAccountUpdate();

step 5

The following will be the output after you click on Execute

click on Execute

As you can see, the status Success means that the account details have been updated.

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce Training.

Come to Intellipaat’s Salesforce Community if you have more queries on Salesforce!

I hope, you had fun learning Batch Apex! In this section, you learned about Salesforce Apex Classes and Batch Apex in Salesforce and you implemented it as well. In the next section of this Salesforce tutorial, you will be learning about Workflow Rules in Salesforce.

Recommended Videos

Course Schedule

Name Date
Salesforce Architect 2021-04-24 2021-04-25
(Sat-Sun) Weekend batch
View Details
Salesforce Architect 2021-05-01 2021-05-02
(Sat-Sun) Weekend batch
View Details
Salesforce Architect 2021-05-08 2021-05-09
(Sat-Sun) Weekend batch
View Details

Leave a Reply

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