Batch Apex in Salesforce

Using Batch Apex in Salesforce | The Developer Guide

Batch Apex

Batch Apex is used to process enormous amounts of data or we can say jobs e.g if we have thousands or millions of records to process, which will easily surpass the governor limit, in that case, we will prefer Batch Apex to perform such jobs. Using Batch Apex, you can deal with records non-simultaneously in batches (thus the name, "Batch Apex") to stay inside platform limits. If you have a huge load of records to run, for example, data cleansing or archiving, Batch Apex is undoubtedly your best plan. 

Here is the way Batch Apex works in the engine. Assume you need to deal with 1 million records using Batch Apex. The logic to execute the process is only called once for every batch of records to process each time Batch class is invoked, the job is queued in apex jobs and executed as a discrete transaction. This value appreciates two astonishing advantages: 

Each trade starts with one more arrangement of governor limits, simplifying it to ensure that your code stays inside the governor execution limits. 

On the off chance that one batch fails to measure effectively, any remaining fruitful batch transactions aren't moved back.

dont miss out iconDon't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial

Batch Apex Syntax

The class ought to execute the Database.Batchable interface and comprise the three methods, for example start, execute and finish to make a Batch Apex Class


This method is utilized to gather records or objects from the information base to be passed to the interface technique execute. The Start technique is simply called once at each beginning of a Batch Apex work and it will return either a Database.QueryLocator iterable contains the records or article that should to be passed to the method. 

Usually a QueryLocator takes care of business with an essential SOQL query to make the degree of objects in the batch job. Regardless, in the event that you need to accomplish something insane like loop through the certain results of an API call or pre-measure records going before being passed to the execute method

With the QueryLocator object, especially far for undeniably the extent of records recuperated by SOQL queries is avoided and you would request have the option to up to 50 million records. Regardless, with an Iterable, beyond what many would consider possible for irrefutably the quantity of records recuperated by SOQL queries is at this point approved. 


Plays out the real handling for each chunk or "batch" of information passed to the methodology. The default batch size is 200 records. Chunks of records yet are not guaranteed to execute in the solicitation wherein they are gotten from the start method.  

This method takes the going with: 

  • A reference to the Database.BatchableContext object. 
  • A list of sObjects, as List<sObject>, or a list of characterized sorts. In case you are utilizing a Database.QueryLocator, utilize the returned list.


Used to execute post-preparing endeavors (for instance, sending an email) and is called once after all batches are managed. 

dont miss out iconCheck out another amazing blog by Shweta here: Learn All About the Dashboards in Salesforce

Invoking a Batch Class 

To invoke a batch class, fundamentally launch it and a short time later call Database.executeBatch with the case: 

MyBatchClass myBatchObject = new MyBatchClass(); 


Using State in Batch Apex 

Batch Apex is ordinarily stateless. Each execution of a Batch Apex work is seen as a discrete exchange. For example, 1000 records are prepared utilizing a batch zenith work having a group size of 5 exchanges of 200 records each. 

If you decide Database.Stateful in the class definition, you can stay aware of state across all exchanges. While using Database.Stateful, simply example part factors hold their characteristics between exchanges. Staying aware of state is useful for considering or summarizing records they're ready.


Popular Salesforce Blogs