Skip to content

BatchApexRecipes

pozil edited this page May 14, 2024 · 18 revisions

BatchApexRecipes

Demonstrates the use of the Database.Batchable interface. The methods in this class are called by the system as the batch executes. To execute this batch use Database.executeBatch(new BatchApexRecipes()); More on the Batchable interface: https://sfdc.co/batch_interface

Implemented types

Database.Batchable<SObject> , Database.Stateful

Group Async Apex Recipes

Fields

private successesList<Id>

private failuresList<Id>

private queryStringString

private resultString

TESTVISIBLE

private throwErrorBoolean

TESTVISIBLE


Methods

public Database start(Database context)

This method is required by the Batchable interface. It's responsible for identifying the records that will be affected Your start method can either return a QueryLocator or an Iterable (List) The records identified here will be made available to the execute method below, in batches of up to 200 records at a time.

Parameters

Param Description
context dependency injected by the system

Returns

Type Description
Database Database.QueryLocator QueryLocator object used for context

Example

Database.executeBatch(new BatchApexRecipes());

public void execute(Database context, List<Account> scope)

This method is where the actual work occurs. It's run once per batch.

Parameters

Param Description
context dependency injected by the system in this batch. It's this mechanism of breaking a large number of records into smaller batches called scope (in this example) that make this easier.
scope a list of up to 200 SObject records to be processed

Example

Database.executeBatch(new BatchApexRecipes());

public void finish(Database context)

This method is called by the system when all the individual batches have completed. Intrepid developers may send emails, or otherwise notify others of the job's completion here.

Parameters

Param Description
context dependency injected by the system

Example

Database.executeBatch(new BatchApexRecipes());

Clone this wiki locally