0% found this document useful (0 votes)
12 views

Batch Apex

Uploaded by

ben sampu
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)
12 views

Batch Apex

Uploaded by

ben sampu
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/ 32

- From the Force.

com Apex Code Developer’s Guide: “Employ Batch Apex to build


complex, long-running processes.”

● Non-Real-Time: Reserved for routines that are not required to provide immediate results.

● High Volume: Can handle a greater volume of records compared to Triggers or Controllers.

● Scheduled: Batch runs are usually scheduled to run periodically at a set time.

● Batch Apex is defined as a class implementing Database.Batchable interface.


● start() - Called at the beginning of the batch operation. Returns either an instance of
Database.QueryLocator or Iterable class which provides the records for use in the execute()
method. Called only once.
● execute() - Called for each batch of records collected by the start() method. A batch may
contain up to 200 records at a time. The size of the batch is controlled by the
Database.executeBatch() invocation. May be called multiple times depending on the
volume of records to be processed.
● finish() - Called after all of the records have been passed to the execute() method. Used for
post-processing operations. Called only once. OWD us set to either “Private” or “Public
Read Only” and if the Custom Object is NOT a child on a Master-Detail Relationship
● The Database.QueryLocator version of Batch Apex is used when the scope of the batch is
straightforward SELECT statement, while the Iterable version is used when the scope of
the batch may only be derived using complex methods.

● The signature of a Batch Apex class using a Database.QueryLocator always includes


“implements Database.Batchable” where “sObject” should not be replaced with a
concrete sObject.

● The signature of an Iterable Batch Apex class includes the following pattern
“implements Database.Batchable<parameterType>” where “parameterType”
follows the parameter type defined on the Iterable class returned by the start() method.
● The Database.BatchableContext object that is found as a parameter on all of the
Database.Batchable methods provides access to the information about the current batch
job:
■ getJobId() - Returns the ID of the AsyncApexJob record associated with this batch
which may be used to track the progress of the batch job.
● The Batch Apex can be ran using the Database.executeBatch() method which accepts the
following parameters:
■ batchableObject - (Required) An instance of a class that implements the
Database.Batchable interface.
■ scope - (Optional) An Integer value that must be greater than 0. If not supplied, the
records are provided to the execute() method in batches of 200 records at a time. This
parameter may be used to restrict the size of a batch to prevent it from reaching the
Governor Limits.

Example:
Once ran, a Batch Apex’s progress is documented under the “Setup” menu’s “Apex Jobs”
page: (Setup | Monitor | Jobs | Apex Jobs)
● The Batch Apex can be ran on a schedule by using another class that implements the
Schedulable interface and its sole method:

■ execute() - The code block that executes every time the class’ schedule run comes up
● Using the “Schedule Apex” button on the Apex Classes page: (Setup | Build | Develop |
Apex Classes)
On the “Schedule Apex” page, provide the necessary details and the click the “Save button”.
● Using the System.schedule() method. The method accepts the following parameters:
■ jobName - (Required) A string that represents the scheduled process.
■ cronExpression - (Required) A string representing the schedule of the job. The value
must follow a specific format.
■ schedulableClass - (Required) An instance of a class that implements the
Schedulable interface.

Example:
The cronExpression value must follow the format, where each section is separated by a
space:

From the example

The following are sample expressions and their resulting schedules:


All scheduled processed, Apex or not, are logged under “Setup” menu’s “All Scheduled
Jobs” page: (Setup | Monitor | Jobs | Scheduled Jobs)
CATEGORY LIMIT
Maximum number of Asynchronous Apex method executions 250,000 -OR- (200 x No. of
per a 24-hour period User Licenses)
Maximum number of Apex classes scheduled concurrently 100
Maximum number of Batch Apex Jobs queued or active 5
Maximum number of records returned for a Batch Apex Query 50 Million
in Database.QueryLocator
● Simulate the batch execution: Execute the Batch Apex directly to get immediate results
using Database.executeBatch(). No need to schedule the process and wait for its
execution.
● Use Test.startTest() and Test.stopTest(): Batch Apex is an asynchronous process. The
methods of the Test Class will guarantee that the Batch Apex runs with its own set of
Governor Limits and that before the code for test verification runs, the Batch Apex has
finished executing.
● Create bulk test data: Batch Apex are made to work with multiple records at a time. Create
data enough for ONLY 1 EXECUTION. Do not create too much data that forces the
execute() method to run multiple times.
● Simulate the batch execution: Schedule the process directly using System.schedule(). No
need to wait for the scheduled process to execute.
● Use Test.startTest() and Test.stopTest(): The methods of the Test Class will guarantee
that the scheduled process runs with its own set of Governor Limits and that before the
code for test verification runs, the process has been scheduled.
● Verify the details off the scheduled process: The System.schedule() method returns the
Scheduled Job ID of the process which may be used to get the details of the schedule. The
ID is from the CronTrigger standard object.
1. When should Batch Apex be used?

2. Batch Apex uses the interface _____ while scheduled processes use the _____ interface.

3. When is the Iterable version of Batch Apex used?

4. What is used to run a Batch Apex?

5. When will a scheduled process run if it was given the Cron Expression ‘0 0 17 ? * FRI’?

6. What is the maximum number of records that a Batch Apex’s execute() method can
receive as its scope?

You might also like