Step 3: Bulk Insert
This Bulk API 2.0 example guides you through creating a job, uploading data for the job, notifying Salesforce servers that your upload(s) are complete, checking the status of the processing job, and retrieving the results.
To do any Bulk API 2.0 task, such as inserting or updating records, you first create a Bulk API 2.0 job. The job specifies the type of object that you’re loading, such as Account, and the operation that you’re performing, such as insert or delete. After you create the job, you use the resulting job ID in subsequent requests to upload job data or abort (cancel) the job.
-
Copy this CSV formatted list of accounts into a file named bulkinsert.csv. You use this file to upload data after creating the job.
Save all files in this example in your terminal’s current working directory.
The first row of the CSV file lists the field names for the object that you’re working with. Each subsequent row corresponds to a record that you want to insert.
-
Create a job.
-
Create a file named
newinsertjob.json. -
Copy this content into the file.
When you create a Bulk API 2.0 job, specify a line ending that matches the line ending used to create the CSV file using the
lineEndingrequest field. Bulk API 2.0 supports two line-ending formats: linefeed (LF), and carriage-return plus linefeed (CRLF). The default line ending, if not specified, isLF. Different operating systems use different characters to mark the end of a line.Unix / Linux / OS X uses
LF(line feed, ‘\n’,0x0A).Windows / DOS uses
CRLF(carriage return followed by line feed, ‘\r\n’,0x0D0A).It’s also possible that the text editor used to create the CSV file is configured for a specific line-ending format that supersedes the default operating system format.
URI
Example for creating a bulk insert job
Example response body
The response includes the job
id, with a jobstateofOpen.You use the job
idfrom this response in the next steps. You can also use the URI in thecontentUrlfield in the next step when you upload your data.
-
-
Upload your CSV data using the URI in the
contentUrlfield of the response.You can upload up to 150 MB per job (after base64 encoding).
The URI is similar to
Example for uploading data
Example response body
No response body.
-
Set job
statetoUploadComplete.After you’re done submitting data, notify Salesforce servers that the upload of job data is complete and is ready for processing.
URI
Example of setting
statetoUploadCompleteExample response body
-
Check the job status and results.
URI
Example of checking job status
Example response body
-
Get successful results.
After a job is in the
JobCompleteorFailedstate, you can get details about which records were successfully processed.URI
Example of getting successful results
The response contains CSV formatted data, with each row containing a record ID (
sf__Id) and information on whether that record was successfully processed or not (sf__Created).Example response body
To get details about records that encountered an error during processing, use a GET request using the
failedResultsresource. To make sure that you’re looking at the complete result set, look at unprocessed records by using theunprocessedRecordsresource. See Get Job Unprocessed Record Results.