Batch Apex Example in Salesforce
Batch Apex Example in Salesforce
Batch class in salesforce is used to run large jobs (think thousands or millions 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. If you
have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably
your best solution.
Here’s how Batch Apex works under the hood. 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
start
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 records are divided into
subtasks and pass those to execute method.
Used to collect the records or objects to be passed to the interface method execute 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
want to check out 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 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
Execute Method performs an operation which we want to perform on the records fetched from
start method.
Performs the actual processing for each chunk or “batch” of data passed to the method. 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.
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.
finish
Finish method executes after all batches are processed. Use this method to send confirmation
email notifications
Used to execute post-processing operations (for example, sending an email) and is called once
after all batches are processed.
Syntax
To use batch Apex, write an Apex class that implements Database.Batchable interface and make
your class global
start()
execute()
finish()
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
Id batchId = Database.executeBatch(myBatchObject);
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. Pro tip: you might want to limit this
batch size if you are running into governor limits.
Each batch Apex invocation creates an AsyncApexJob record so that you can track the job’s
progress. You can view the progress via SOQL or manage your job in the Apex Job Queue. We’ll
talk about the Job Queue shortly.
can also use the Schedulable interface with batch Apex classes. The following example
implements the Schedulable interface for a batch Apex class called batchable:
Test class for batch Apex