Sub topic 1
Loading Large Data Volumes with Bulk API 2.0
Prerequisites
Assume you understand Salesforce objects, fields, record IDs, external IDs, REST methods, JSON, and CSV. You also have an OAuth access token for a Salesforce org.
When Bulk API 2.0 is the right tool
Bulk API 2.0 is an asynchronous REST API for processing large sets of Salesforce records. You submit a job, upload data, and check the result later. Salesforce processes the records in the background.
Use it for large inserts, updates, upserts, deletes, and exports. Use the regular REST API when you need an immediate response for one record or a small request. Bulk API 2.0 accepts CSV rather than one JSON request per record.
Authentication and endpoint structure
Send the OAuth token in an Authorization header:
Authorization: Bearer ACCESS_TOKENBulk endpoints use your Salesforce instance URL and a REST API version:
https://INSTANCE_URL/services/data/v62.0/jobs/ingestReplace v62.0 with an API version supported by your org. The /jobs/ingest resource handles data changes. The /jobs/query resource handles bulk exports.
The job model and job states
A job records the operation, target object, format, state, and record counts. A newly created ingest job is usually Open. After you upload the CSV, change its state to UploadComplete. Salesforce then starts processing it.
Typical terminal states are JobComplete, Failed, and Aborted. JobComplete can still include individual failed records, so always retrieve the result files.
> callout note > Closing the upload is a separate request. Uploading CSV alone does not start processing.
Preparing your CSV file
Use field API names in the header row. Each later row represents one record. Quote values containing commas, newlines, or quotation marks. Keep the encoding and line endings consistent with the job settings.
FirstName,LastName,Email
Ada,Lovelace,[email protected]
Alan,Turing,[email protected]Creating an ingest job
Specify the Salesforce object, operation, content type, and line ending. The response returns the job ID, which you use for later requests.
curl -X POST "$INSTANCE_URL/services/data/v62.0/jobs/ingest" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"object":"Contact","operation":"insert","contentType":"CSV","lineEnding":"LF"}'Exercise
Which request changes an uploaded ingest job from waiting to processing?
Show answer
A PATCH request that sets the job state to UploadComplete.