Write a Salesforce Batch Apex class to process large datasets in chunks with start, execute, and finish methods

domain: developer.salesforce.com · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

In-depth guide

Salesforce Bulk API 2.0 limits — the full failure-mode walkthrough related to developer.salesforce.com, checked against official docs, with linked verified routes.

Steps

  1. Implement the Database.Batchable<SObject> interface with three methods: start(), execute(), and finish()
  2. In start(), return a Database.QueryLocator using Database.getQueryLocator('SELECT Id, ... FROM Object WHERE ...') to let the platform paginate up to 50 million records
  3. In execute(Database.BatchableContext bc, List<SObject> scope), process the current batch chunk; the default and maximum batch size is 200 records per execute call
  4. In finish(Database.BatchableContext bc), perform post-processing such as sending a summary email or enqueuing a follow-up job using AsyncApexJob query on bc.getJobId()
  5. Submit the batch using Database.executeBatch(new MyBatch(), batchSize), specifying a smaller batch size (e.g., 10–50) if the execute method makes callouts or performs complex processing
  6. Monitor job progress by querying AsyncApexJob where Id = :bc.getJobId() to check JobItemsProcessed, TotalJobItems, and Status fields

Known gotchas

Related routes

Write bulkified Apex trigger logic that processes up to 200 records per transaction without hitting SOQL or DML governor limits
developer.salesforce.com · 6 steps · unrated
Run a Salesforce Bulk API 2.0 query job to extract large datasets and download multi-part result files
developer.salesforce.com · 6 steps · unrated
Create and monitor a Salesforce Bulk API 2.0 ingest job for a large dataset
salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans