What Is Batch Apex in Salesforce?

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

Watch this video on Salesforce by Intellipaat:

Batch Apex in Salesforce What Is Batch Apex in Salesforce? In this section, we will understand about Batch Apex in Salesforce. Consider a situation wherein you have to process a large amount of data on a daily basis and have to delete unused data. It would be difficult to manually do so. This is


Before getting deep into this topic, let’s see the topics we’ll be covering in this section:

Batch Apex in Salesforce is specially designed for a large number of records, i.e., the record 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 limits.
  • Until a batch is not successfully executed, Batch Apex won’t execute or even consider executing 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 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: A 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 encounter 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 Database. The batchable interface needs to be implemented first. It has the following three methods:

  • Start: This method is called at the starting of the 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 this method 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 at the end, it is responsible to perform post-processing operations like sending an email. When this process is called all the batches are already executed.

Syntax:

global void finish(Database.BatchableContext BC) {}

Creating 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: When 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 which 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) {

    }

}


Step 4:
After writing the code, you’ll have to go to the 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();
database.executeBatch(b);

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.

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

I hope, you had fun learning Batch Apex! In this session, we learned about Salesforce Apex Classes, Batch Apex in Salesforce and we tried to implement it as well. In the next session of this Salesforce tutorial, we will be learning about Workflow in Salesforce.

Recommended Videos

Leave a Reply

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

Solve : *
5 × 25 =