Sub topic 7
Batch sync: moving large volumes with Bulk API 2.0
This chapter assumes you know the Salesforce data model, can read REST and JSON, and understand HTTP methods and status codes. You should also have read the earlier chapters on synchronous and asynchronous trade-offs and on Named Credentials.
When batch sync fits
Batch sync moves many records in one operation, on a schedule or when a large load is needed. Nobody waits for each record. The caller submits a job, Salesforce processes it in the background, and the caller checks the result later.
Typical uses are:
- Nightly loads from an ERP system into Salesforce.
- Initial data migrations of hundreds of thousands or millions of records.
- Periodic exports of large datasets to a data warehouse.
Request-reply calls cost one API request per record. At this volume that is too slow and uses too much of your daily API allocation. Bulk API 2.0 handles the same volume in a few requests.
How Bulk API 2.0 works
Bulk API 2.0 is a REST API for asynchronous jobs. You do not manage batches yourself. You upload one CSV, and Salesforce splits it into internal batches.
An ingest job (a job that writes data into Salesforce) goes through these states:
Step 1: create the job
This request creates an upsert job for Accounts. An upsert updates a record if it exists and inserts it if it does not. The match is made on an external ID field.
POST /services/data/v60.0/jobs/ingest
Content-Type: application/json
Authorization: Bearer <access_token>
{
"object": "Account",
"externalIdFieldName": "External_Id__c",
"contentType": "CSV",
"operation": "upsert",
"lineEnding": "LF"
}The response contains a job id and a contentUrl. You use both in the next steps.
Step 2: upload data and close the upload
Send the CSV with a PUT to the job's batches endpoint, using Content-Type: text/csv. The header row must name fields by API name, and it must include External_Id__c:
External_Id__c,Name,Industry
ERP-10042,Acme Corporation,Manufacturing
ERP-10043,Globex Inc,EnergyWhen the upload is finished, send a PATCH to the job URL with {"state": "UploadComplete"}. Salesforce does not start processing until you do this. A common mistake is to upload the file and then wait for a job that never starts.
Step 3: poll and read results
Send a GET to the job URL and check state. When it is JobComplete, fetch three result sets:
| Endpoint | Content |
|---|---|
| `successfulResults` | Rows that were saved, with the new record ID and a created flag |
| `failedResults` | Rows that failed, with an error message |
| `unprocessedrecords` | Rows that were never attempted, for example after an abort |
JobComplete does not mean every row succeeded. A job can finish with some failed rows. Always read failedResults and compare numberRecordsFailed with zero.
Why the external ID matters
The external ID is what makes the job safe to repeat. If a network error forces you to run the same file twice, existing rows are updated and no duplicates appear. The same idea applies to a single record through the REST API:
PATCH /services/data/v60.0/sobjects/Account/External_Id__c/ERP-10042
Content-Type: application/json
Authorization: Bearer <access_token>
{
"Name": "Acme Corporation",
"Industry": "Manufacturing"
}Mark the field as an external ID, and make it unique where the source system guarantees uniqueness. Chapter material on idempotency covers this in more depth.
Common errors
- Forgetting
UploadComplete. The job staysOpenand nothing is processed. - Wrong column names. Headers must be field API names. Custom fields end in
__c. - Treating
JobCompleteas success. CheckfailedResults. - Ignoring triggers and flows. Bulk loads still fire them, so slow automation can cause lock errors and timeouts. Consider bypassing non-essential automation during a migration, with a safe, approved mechanism.
- Loading children before parents. Load parent objects first, or reference them by external ID, so relationship lookups resolve.
- Hitting limits. Bulk API 2.0 has daily limits on records processed and on file size per job. Split very large loads and check the current limits for your org.
Exercise
Your nightly job uploads a 200,000-row CSV to a Bulk API 2.0 ingest job, but the job stays in the Open state for hours. What is the most likely cause, and how do you fix it? Also, why is an upsert on an external ID a good choice if you must rerun the job?
Show answer
The client never set the job state to UploadComplete, so Salesforce never started processing. Send a PATCH to the job URL with {"state": "UploadComplete"} after the upload. An external ID upsert updates records that already exist and inserts only new ones, so rerunning the same file does not create duplicates.