Newer Version Available

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

Step 4: Bulk Insert with a Multipart Request

This Bulk API 2.0 example guides you through creating a job, uploading data for the job, checking the status, and retrieving the results. This example uses a single, multipart request to create the job and upload the data.

In this example, BOUNDARY is used at the start, middle, and end of the file to mark the request body boundaries between the job’s details and its CSV data.

  1. Copy this multipart, JSON-formatted content into a file named newmultipartjob.json.

    Save all files in this example in your terminal’s current working directory.

    Note

    1--BOUNDARY
    2Content-Type: application/json
    3Content-Disposition: form-data; name="job"
    4
    5{
    6  "object":"Contact",
    7  "contentType":"CSV",
    8  "operation": "insert",
    9  "lineEnding" : "LF"
    10}
    11
    12--BOUNDARY
    13Content-Type: text/csv
    14Content-Disposition: form-data; name="content"; filename="content"
    15
    16FirstName,LastName,MailingCity
    17Astro,Nomical,San Francisco
    18Hootie,McOwl,San Francisco
    19Appy,Camper,San Francisco
    20Earnie,Badger,San Francisco
    21--BOUNDARY--
  2. Create a job.

    URI

    1/services/data/v59.0/jobs/ingest/

    Example of creating a job

    1curl https://MyDomainName.my.salesforce.com/services/data/v59.0/jobs/ingest/ -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "Content-Type: multipart/form-data; boundary=\"BOUNDARY\"" -H "Accept: application/json" -H "X-PrettyPrint:1" --data-binary @newmultipartjob.json -X POST

    The response includes the job id, with a job state of UploadComplete. You use the job id in the next steps.

    Example response body

    1{
    2  "id" : "7303gEXAMPLE4X2QAN",
    3  "operation" : "insert",
    4  "object" : "Contact",
    5  "createdById" : "0055fEXAMPLEtG4AAM",
    6  "createdDate" : "2022-01-02T19:26:52.000+0000",
    7  "systemModstamp" : "2022-01-02T19:26:52.000+0000",
    8  "state" : "UploadComplete",
    9  "concurrencyMode" : "Parallel",
    10  "contentType" : "CSV",
    11  "apiVersion" : 59.0,
    12  "lineEnding" : "LF",
    13  "columnDelimiter" : "COMMA"
    14}
    After you create a multipart job, the upload is completed for you automatically. You don’t need to manually set the job state to UploadComplete for a multipart job.
  3. Check the job status and results with this URI.

    URI

    1/services/data/v59.0/jobs/ingest/jobId/

    Example of checking job status and results

    1curl https://MyDomainName.my.salesforce.com/services/data/v59.0/jobs/ingest/7303gEXAMPLE4X2QAN/ -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "Accept: application/json" -H "X-PrettyPrint:1" -X GET

    Example response body

    1{
    2  "id" : "7303gEXAMPLE4X2QAN",
    3  "operation" : "insert",
    4  "object" : "Contact",
    5  "createdById" : "0055fEXAMPLEtG4AAM",
    6  "createdDate" : "2022-01-02T19:54:04.000+0000",
    7  "systemModstamp" : "2022-01-02T19:54:05.000+0000",
    8  "state" : "JobComplete",
    9  "concurrencyMode" : "Parallel",
    10  "contentType" : "CSV",
    11  "apiVersion" : 59.0,
    12  "jobType" : "V2Ingest",
    13  "lineEnding" : "LF",
    14  "columnDelimiter" : "COMMA",
    15  "numberRecordsProcessed" : 4,
    16  "numberRecordsFailed" : 0,
    17  "retries" : 0,
    18  "totalProcessingTime" : 50,
    19  "apiActiveProcessingTime" : 6,
    20  "apexProcessingTime" : 0
    21}
  4. Get successful results.

    After a job is in the JobComplete or Failed state, you can get details about which records were successfully processed.

    URI

    1/services/data/v59.0/jobs/ingest/jobId/successfulResults/

    Example of getting successful results

    1curl https://MyDomainName.my.salesforce.com/services/data/v59.0/jobs/ingest/7303gEXAMPLE4X2QAN/successfulResults/ -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "Content-Type: application/json" -H "Accept: text/csv" -H "X-PrettyPrint:1" -X GET

    Example response body

    1"sf__Id","sf__Created",FirstName,LastName,MailingCity
    2"0038c00002hMS4kAAG","true","Astro","Nomical","San Francisco"
    3"0038c00002hMS4lAAG","true","Hootie","McOwl","San Francisco"
    4"0038c00002hMS4mAAG","true","Appy","Camper","San Francisco"
    5"0038c00002hMS4nAAG","true","Earnie","Badger","San Francisco"

    To get details about records that encountered an error during processing, use a GET request with 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.