IntermediateLesson 23 of 37
Asynchronous Apex
Move slow or heavy work out of the user's transaction.
- @future: simple fire-and-forget (primitives only).
- Queueable: like future but with objects, chaining and monitoring.
- Batch Apex: process up to millions of records in chunks (default 200).
- Schedulable: run on a schedule.
apex
public class RecalcBatch implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id FROM Account');
}
public void execute(Database.BatchableContext bc, List<Account> scope) { /* work */ }
public void finish(Database.BatchableContext bc) {}
}
Database.executeBatch(new RecalcBatch(), 200);