Learn All About the Batch Apex in 2023

Learn All About the Batch Apex in 2023

The main purpose of using Batch Apex is used to process a large amount of data without exceeding governor limits. It is a type of Asynchronous Apex which is used to run processes in a separate thread at a later time. This is a process that executes a task in the background without the user having to wait for the task to finish. Batch Apex gives the facility of the asynchronous processing of records in multiple chunks. This is limited to five tasks to run simultaneously. 

Using Batch Apex

To use Batch Apex you have to write an Apex class that implements Database.Batchable Interface. To Track the batch Apex job, from Setup, enter Apex Jobs, then select Apex Jobs. 

Implementation of Database.Batchable interface

Database.Batchable interface contains three methods to be implemented 

Start Method

Public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext  ac) {} 

The start method is used to collect the records or object to pass to the interface method Execute. This method returns either Database.QueryLocator or an Iterable object. 

When you want a simple query like [select]  then you use Database.QueryLoactor and when you want some complex scope of the object then you use Iterable object. 

By using Database.QueryLocator the governor limit for total records retrieved by soql queries is bypassed. 

dont miss out iconDon't forget to check out: Batch Apex in Salesforce - Learn About the Advantages and Syntax

Execute Method

Public void execute(Database.Batchable context ac, list<sObject>){} 

In the execute method, you do all the required processing for each chunk of data. 

This method takes the following two parameters: 

  • A reference to the Database.BatchableContext object. 
  • List of sObjects. 

Finish Method

Public void finish (Database.BatchableContext ac) {} 

Eexecute all the after-processing and sending confirmation Emails using the finish method. 

Using  Database.QueryLocator 

The following example uses Database.QueryLocator:- 

Using Iterable in Batch Apex to Define Scope

The following example uses Iterable:- 

dont miss out iconCheck out another amazing blog by Saurabh here: What are Flows In Salesforce in 2023?

What are the Best Practices for Batch Apex?

  • If you are planning to invoke a batch job from a trigger extreme care, you should be able to guarantee that the trigger does not add more batch jobs than the limit. Particularly, consider API bulk updates, import wizards, and mass record changes through the user interface, and all cases where more than one record can be updated at a time. 
  • Whenever we call Database.executeBatch, Salesforce only places the job in the queue. Actual execution will be based on the service availability. 
  • By testing batch Apex, you can only test one execution of the execute method. Use the scope parameter to ensure that you aren’t running into governor limits. 
  • The method which is declared as future isn’t allowed in classes that implement the Database.Batchable interface.
  • The batch jobs which executed before a salesforce service maintenance downtime remain in the queue.
  • The Methods which declared as future can’t be called from a batch Apex class.
  • Batches run in default size of 200 records per chunk, and all the batches have their own governor limits for processing.
  • If it is possible then use the minimum number of Batches.
  • You can test only one execution of the execute method when testing your batch.
  • You Can submit up to 5 batch jobs at running time .
  • Database.QueryLocator can return a maximum of 50 million records.  

Responses

Popular Salesforce Blogs