Batch Apex

Defination of Batch Apex

Batch Apex in Salesforce is a way to process large amounts of records asynchronously. It allows you to break down operations into smaller, manageable chunks instead of hitting governor limits when dealing with bulk data.

Key future of Batch Apex-

  1. Handles Large Data Volumes – Can process up to 50 million records.

  2. Runs Asynchronously – Runs in the background without blocking user interactions.

  3. Efficiently Bypasses Governor Limits – Executes in small batches (default 200 records per batch), reducing limits on CPU time, queries, and DML operations.

  4. Supports Callouts – Can make external web service calls if Database.AllowsCallouts is implemented.

  5. Can be Scheduled – Works with System.scheduleBatch() to automate execution.

Basic Structure of Batch Apex

To implement Batch Apex, you must implement the Database.Batchable interface, which has three methods:

  1. start – Gathers the records to be processed.

  2. execute – Processes each batch of records.

  3. finish – Performs final actions like logging or sending emails.