Batch Apex in Salesforce
It allows you to define a job that can be divided into manageable chunks, where each chunk can be proceed separately.
For example, if you want to make an field update of all records in any object which is having more number of records, then governor limits restricts us to process that operation. Because, In a single transaction we can only process 10,000 records.
In batch apex, it will fetch all records which you want perform the field update and divide them into list of 200 records & every 200 records operation is performed separately.
To use Batch Apex, you must implement “Database.Batchable”. This interface has three methods. those are:
1. Start
2. execute
3. Finish
1. Start
2. execute
3. Finish
Start method is
automatically called at the beginning of the apex job. This method will
collect record or objects on which the operation should be performed.
These record are divided into subtasks & passes those to execute
method.
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
This method returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed into the job.
Query executed in the start method will return maximum 5,00,00000 records in a transaction.
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
This method returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed into the job.
Query executed in the start method will return maximum 5,00,00000 records in a transaction.
Execute Method performs operation which we want to perform on the records fetched from start method.
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name + 'Updated';
}
update scope;
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name + 'Updated';
}
update scope;
}
Batches of records are not guaranteed to execute in the order they are received from the start method.
Finish method executes after all batches are processed. Use this method to send confirmation email notifications.
global void finish(Database.BatchableContext BC) {
}
Finish method executes after all batches are processed. Use this method to send confirmation email notifications.
global void finish(Database.BatchableContext BC) {
}
Apex Code:
global class batchAccountUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name + 'Updated';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}
Batch Apex Governor Limits:-
global class batchAccountUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name + 'Updated';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}
Batch Apex Governor Limits:-
- All methods in the class must be defined as global or public.
- Up to 5 batch jobs can be queued or active concurrently.
- In a running test, you can submit a maximum of 5 batch jobs.
- The maximum number of batch Apex method executions per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater.
- You cannot use the getContent and getContentAsPDF PageReference methods in a batch job.
- The start, execute, and finish methods can implement up to 10 callouts each.
- In a running test, you can submit a maximum of 5 batch jobs.
- A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
- Start method can have up to 15 query cursors open at a time per user.
- Execute and finish methods each have a limit of five open query cursors per user.
Disadvantages of Batch Apex:
1) It runs asynchronously, which can make it hard to troubleshoot without some coded debugging, logging, and persistent stateful reporting. It also means that it's queued to run, which may cause delays in starting.
2) There's a limit of 5 batches in play at any time, which makes it tricky to start batches from triggers unless you are checking limits.
3) If you need access within execute() to some large part of the full dataset being iterated, this is not available. Each execution only has access to whatever is passed to it, although you can persist class variables by implementing Database.stateful.
4) There is still a (fairly large) limit on total Heap size for the entire batch run, which means that some very complex logic may run over, and need to be broken into separate batches.
Comments
Post a Comment