Sub topic 7
Bulk API 2.0 for large data volumes
This chapter assumes you can already authenticate with OAuth 2.0, write basic SOQL, and call the REST API. It covers when and how to load or extract large record sets with Bulk API 2.0.
Why a separate API for volume
The REST API handles one request at a time and returns a result right away. That works well for single records or small sets. For tens of thousands to millions of records, it runs into two problems: each call counts against your API limits, and the round trips take a long time.
Bulk API 2.0 is asynchronous. You describe a job, upload a CSV file, and Salesforce processes the data in the background. You then poll for the result. Salesforce splits the data into batches internally, so you don't manage batches yourself. That is the main difference from the original Bulk API.
A rough rule: consider Bulk API 2.0 when you move more than about 2,000 records at a time. Below that, REST or Composite requests are usually simpler.
The ingest job lifecycle
An ingest job moves through a fixed set of states.
The three calls below create a job, upload the data, and close the upload. Replace JOB_ID with the id value returned by the first call.
# 1. Create the ingest job
curl -X POST "https://MyDomain.my.salesforce.com/services/data/v60.0/jobs/ingest" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"object": "Contact", "operation": "insert", "contentType": "CSV", "lineEnding": "LF"}'
# 2. Upload the CSV data (JOB_ID comes from the response above)
curl -X PUT "https://MyDomain.my.salesforce.com/services/data/v60.0/jobs/ingest/$JOB_ID/batches" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: text/csv" \
--data-binary @contacts.csv
# 3. Tell Salesforce the upload is finished so processing starts
curl -X PATCH "https://MyDomain.my.salesforce.com/services/data/v60.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"state": "UploadComplete"}'The CSV header row must use field API names, for example FirstName,LastName,Email. The lineEnding value in the job must match the file. A mismatch is a common cause of a job that fails or parses to one giant row.
Checking results
Processing starts only after you send UploadComplete. Poll GET /services/data/v60.0/jobs/ingest/$JOB_ID until state is JobComplete, Failed, or Aborted. The response includes numberRecordsProcessed and numberRecordsFailed.
A job that finishes as JobComplete can still contain failed rows. Fetch the per-record outcomes from these endpoints under the job:
| Endpoint | Contents |
|---|---|
| `/successfulResults` | Rows that were saved, with the new record Id |
| `/failedResults` | Rows that failed, with an error message |
| `/unprocessedrecords` | Rows never attempted, such as after an abort |
Always read failedResults. Fix the rows and submit them as a new job.
Operations and queries
The operation field accepts insert, update, upsert, delete, and hardDelete. For upsert, also set externalIdFieldName so Salesforce can match existing records. Using an external Id makes reruns safe, because a retried row updates instead of duplicating.
Bulk API 2.0 also supports query jobs at /jobs/query. You post a SOQL statement, poll the job, and download the results as CSV. This suits exports of large tables that would otherwise need many paged REST queries.
Limits and design notes
- Bulk jobs count against daily limits on records and batches, which are separate from normal REST call counts. Check your org's current numbers.
- Each upload is limited in size, so split very large files across multiple jobs.
- Records in a job are processed in parallel, so row-lock errors can occur when many rows touch the same parent record. Sort the CSV by parent Id to reduce them.
- Triggers, flows, and validation rules still run. A heavy automation setup slows the job.
Exercise
You must load 500,000 contacts nightly and rerun the job safely if it fails halfway. Which API and operation do you choose, and why?
Show answer
Use Bulk API 2.0 with the upsert operation and an external Id field set in externalIdFieldName. The volume is too large for efficient REST calls. Upsert matches on the external Id, so rerunning updates existing rows instead of creating duplicates.