html code

Using Batch Apex as described in the Salesforce Apex Developer Guide.

Cluster Apex is employed for the execution of extensive operations, involving a substantial volume of records (think in terms of thousands or even millions!). It serves as a solution for processing tasks that would otherwise exceed the conventional processing limits. Through Batch Apex, you can process records in clusters in a non-concurrent manner (hence the term “Batch Apex”), ensuring compliance with platform limits. When dealing with a significant number of records, such as data cleansing or archiving, Batch Apex is the recommended approach.

Here’s how Batch Apex functions under the hood: Let’s consider a scenario where you intend to handle one million records using Batch Apex. The execution logic within the batch class is invoked for each batch of records being processed. Each invocation of a batch class places the job onto the Apex job queue and executes it as an isolated transaction. This functionality offers two remarkable advantages:

  • Every transaction begins with a fresh set of governor limits, simplifying the task of ensuring that your code adheres to governor execution constraints.
  • If one batch fails to process successfully, the successful batch transactions remain unaffected and are not rolled back.

Syntax for Batch Apex

To create a Batch Apex class, your class should implement the Database.Batchable interface and include the following three methods:

Initiate

Utilized for collecting records or addressing issues that need to be conveyed to the execute method in the interface for processing. This method is invoked once at the outset of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable containing the records or objects to be processed within the job.

Typically, a QueryLocator suffices with a straightforward SOQL query to establish the scope of items within the batch job. However, if you need to perform something unconventional like iterating through the results of an API call or pre-processing records prior to passing them to the execute method, you should explore the Custom Iterators link in the Resources section.

With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed, allowing you to query up to 50 million records. Nonetheless, when using an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

Process

Executes the actual processing for each batch or “chunk” of data handed over to the method. The default batch size is set at 200 records. The execution order of record batches is not guaranteed to match the order in which they were received from the initial method.

This method requires the following:

This method necessitates the following:

  • A reference to the Database.BatchableContext object.
  • A collection of sObjects, such as List<sObject>, or a collection of user-defined types. If you are employing a Database.QueryLocator, use the returned list.
  • The finish method, employed for executing post-processing tasks (e.g., sending an email), is invoked once after all batches have been processed.

Here is how the basic structure of a Batch Apex class looks:

public class MyBatchClass implements Database.Batchable&lt;sObject&gt; {
    public (Database.QueryLocator | Iterable&lt;sObject&gt;) start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
    }
    public void execute(Database.BatchableContext bc, List&lt;P&gt; records){
        // process each batch of records
    }
    public void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }
}

Optimal Approaches

Similarly to future methods, there are certain considerations to bear in mind when utilizing Batch Apex. To ensure swift execution of batch jobs, it’s essential to minimize web service callout times and optimize queries employed in your batch Apex code. Prolonged execution of batch jobs can potentially lead to delays in queued jobs, particularly when numerous jobs are in the queue. Some best practices encompass:

  • Reserve the use of Batch Apex for scenarios involving multiple batches of records. If you don’t have enough records to justify more than one batch, Queueable Apex might be a more suitable option.
  • Optimize any SOQL queries to swiftly gather the records for processing.
  • Keep the number of asynchronous requests to a minimum to mitigate the risk of delays.
  • Exercise caution if you plan to invoke a batch job from a trigger. It’s crucial to ensure that the trigger won’t exceed the batch job limit.