As platform limit plays a very
important role in Salesforce, it becomes more important to stay within the
platform limits when we are dealing with large number of records. With batch
apex we can process large number of records asynchronously.
Understanding batch apex with
an example would be easier, Let’s try to understand it with a scenario
described below.
Let’s say you want to process 1
million records using Batch Apex. The execution logic of the batch class is
called once for each batch of records you are processing. Each time you invoke
a batch class, the job is placed on the Apex job queue and is executed as a
discrete transaction. Every transaction
starts with a new set of governor limits, making it easier to ensure that your
code stays within the governor execution limits. If one batch fails to process
successfully, all other successful batch transactions aren’t rolled back.
When we use batch apex, we implement
Database.batchable() interface.
The Database.batchable() interface
contains three methods that we need to define inside batch class, and they are
as below:
1)start
2)execute
3)Finish
All the methods in the
Database.Batchable interface require a reference to a Database.BatchableContext
object.
Syntax of batch
class:
global class batch implements Database.Batchable < sObject > {
global (Database.QueryLocator or
Iterable<sObject>)
start(Database.BatchableContext bc) {
//query on object;
//return Database.getQueryLocator(query);
}
global void execute(Database.batchableContext bc, List < SObject >
scope) {
//some processing.
}
global void finish(Database.BatchableContext bc) {
//job such as sending email or calling another batch class
}
}
Start method collects the data for processing. This
method is called once at the beginning of a Batch Apex job and returns either a
Database.QueryLocator object or an Iterable that contains the records or
objects passed to the job.
Most of the time a QueryLocator
does the trick with a simple SOQL query to generate the scope of objects in the
batch job but if you need to do something crazy like loop through the results
of an API call or pre-process records before being passed to the execute
method, you might need to go with the Custom Iterators.
With the QueryLocator object,
the governor limit for the total number of records retrieved by SOQL queries is
bypassed and you can query up to 50 million records. However, with an Iterable,
the governor limit for the total number of records retrieved by SOQL queries is
still enforced.
Execute method performs operations. The default batch size is 200 records. Batches of
records are not guaranteed to execute in the order they are received from the
start method.
This method takes the
following:
A reference to the Database.BatchableContext
object.
A list of sObjects, such as
List<sObject>, or a list of parameterized types. If you are using a
Database.QueryLocator, use the returned list.
Each execution of a batch Apex
job is considered a discrete transaction. For example, a batch Apex job that
contains 1,000 records and is executed without the optional scope parameter
from Database.executeBatch is considered five transactions of 200 records each.
The Apex governor limits are reset for each transaction. If the first transaction
succeeds but the second fails, the database updates made in the first
transaction aren’t rolled back.
Finish method generally used for sending emails or
calling another batch class when the current batch is completed. Using Database.BatchableContext
object we can track the progress of the batch job in finish method as shown
below.
getJobID is the instance method
with the Database.BatchableContext object.
It returns the ID of the
AsyncApexJob object associated with this batch job as a string.
public void finish(Database.BatchableContext BC){
AsyncApexJob a = [SELECT Id,
Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems,
CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
// Send an email to the Apex
job's submitter notifying of job completion.
Messaging.SingleEmailMessage
mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new
String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Batch Job
Status ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed
' + a.TotalJobItems +
' batches with '+
a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new
Messaging.SingleEmailMessage[] { mail });
}
Invoking a Batch
Class:
To invoke a batch class, simply
instantiate it and then call Database.executeBatch with the instance:
SampleBatchClass batchObject =
new SampleBatchClass ();
Id batchId =
Database.executeBatch(batchObject);
You can also optionally pass a
second scope parameter to specify the number of records that should be passed
into the execute method for each batch. The optional scope parameter of
Database.executeBatch can have a maximum value of 2,000. If set to a higher
value, Salesforce chunks the records returned by the QueryLocator into smaller
batches of up to 2,000 records.
Id batchId =
Database.executeBatch(batchObject, 100);
The Database.executeBatch
method returns the ID of the AsyncApexJob object, which you can use to track
the progress of the job.
AsyncApexJob aaj = [SELECT Id,
Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID
=: batchId];
Scheduling apex job:
To Schedule the batch class,
you need a class which implements schedulable interface,
global class schedulebatch implements schedulable{
global void
execute(Schedulablecontext sc)
{
updateNameOfAccount
acc=new updateNameOfAccount();
//Database.executeBatch(instance,size);
Database.executeBatch(acc,200);
}
}
Now, you can schedule class schedulebatch
from User Interface,
Setup>develop>apex classes> schedule apex.
clear explanation thank you
ReplyDeleteFantastic work!
ReplyDeleteThis really helped and clean explanation.
ReplyDeleteClean Explanation .. Thank you
ReplyDeleteWell Explained.... It Means a lot about batch class
ReplyDeleteGreat KB
ReplyDeleteGreat explanation
ReplyDeletecan we declare a method as iterable and will return database.getQueryLocator(query). ??
ReplyDeleteYes
ReplyDeleteLovelyy post
ReplyDelete