Newer Version Available

This content describes an older version of this product. View Latest

Step 3: Bulk Insert

This Bulk API 2.0 example guides you through creating a job to insert new records, uploading data for the job, checking the status, 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 Bulk API 2.0 requests to close or abort (cancel) the job.

  1. 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.

    Note

    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.
  2. Create a job.
    1. Create a file named newinsertjob.json.
    2. Copy this content into the file.

      "LF" is the default lineEnding, and is used to format files on Mac OSX, Linux, and Unix. Use CRLF for Windows or DOS machines.

      URI

      Example for creating a bulk insert job

      Example response body

      The response includes the job id, with a job state of Open.
      You use the job id from this response in the next steps. You can also use the URI in the contentUrl field in the next step when you upload your data.
  3. Upload your CSV data using the URI in the contentUrl field 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.

  4. Close the job.

    After you’re done submitting data, you can inform Salesforce that the job is ready for processing by closing the job.

    URI

    Example of closing the job

    Example response body

  5. Check the job status and results.

    URI

    Example of checking job status

    Example response body

  6. Get successful results.

    After a job is in the JobComplete or Failed state, 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 failedResults resource. To make sure that you’re looking at the complete result set, look at unprocessed records by using the unprocessedRecords resource. See Get Job Unprocessed Record Results.