Batch Apex
Batch Apex
● 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.
● 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:
2. Batch Apex uses the interface _____ while scheduled processes use the _____ interface.
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?