Batch Apex

To use batch Apex, write an Apex class that implements the Salesforce-provided interface Database.Batchable and then invoke the class programmatically.

To monitor or stop the execution of the batch Apex job, from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.

Implementing the Database.Batchable Interface

The Database.Batchable interface contains three methods that must be implemented.

  • start method:
public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}
  • public (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}
  • ) start(Database.BatchableContext bc) {}” style=”font-family: FontAwesome; margin: 0px; vertical-align: baseline; cursor: pointer; appearance: button; display: inline-block; width: 1.25em; height: auto; background-position: 0% 0%; background-repeat: repeat; font-weight: normal; font-style: normal; text-decoration: inherit; -webkit-font-smoothing: antialiased; line-height: normal; background-image: none; text-align: center; border: none; box-shadow: none; outline: none; position: absolute; padding: 0px; top: 5px; right: 7px; color: rgb(112, 110, 107);”>
  • To collect the records or objects to pass to the interface method execute, call the start method at the beginning of a batch Apex job. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job.
  • When you’re using a simple query (SELECT) to generate the scope of objects in the batch job, use the Database.QueryLocator object. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an org.

execute method:

public void execute(Database.BatchableContext BC, list<P>){}

finish method

public void finish(Database.BatchableContext BC){}

The Database.executeBatch method returns the ID of the AsyncApexJob object, which you can use to track the progress of the job. For example:

ID batchprocessid = Database.executeBatch(reassign);

AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID =: batchprocessid ];

Batch Apex is used to process a large number of records that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits.

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

Start method:
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.
Limit: 50 million records can be retrieved using getQueryLocator

Execute method:
This method executes after the Start method, and it does the actual processing for each batch, separately.

Finish method:
This method will be called at last and used to perform post-processing operations.

Points to Remember:
1] Batch apex can call another batch but only from the Finish method.
2] Minimum batch size: 1, Maximum batch size: 2000,
Default batch size: 200
3] If one batch got failed then only that batch records get rolled back, other batches will be processed correctly.
4] Database.stateful: Batch Apex is stateless by default. That means for each execution of your execute method, you receive a fresh copy of your object. If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface.
5] Start and Finish method runs only once but Execute method will run multiple times depending on total records and batch size.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TechGuy

Tech enthusiastic, life explorer, single, motivator, blogger, writer, software engineer