0% found this document useful (0 votes)
119 views6 pages

Batch Apex Example in Salesforce

Uploaded by

Aamir Dehngal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views6 pages

Batch Apex Example in Salesforce

Uploaded by

Aamir Dehngal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Batch Apex Example In Salesforce

What is Batch Apex 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.

Advantage of using batch Apex

● 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

Batch Apex Syntax


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

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.

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.

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

Key points about batch Apex

To use batch Apex, write an Apex class that implements Database.Batchable interface and make
your class global

Implement the following 3 methods

start()

execute()

finish()

The default batch size is 200


Monitoring Batch Apex

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

Monitoring Batch Apex in Anonymous Window


MyBatchClass myBatchObject = new MyBatchClass();

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.

Id batchId = Database.executeBatch(myBatchObject, 100);

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.

SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob WHERE ID


= :batched

Scheduling Batch Apex

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

You might also like