Api Bulk v2
Api Bulk v2
0
Version 46.0, Summer ’19
@salesforcedocs
Last updated: June 24, 2019
© Copyright 2000–2019 salesforce.com, inc. All rights reserved. Salesforce is a registered trademark of salesforce.com, inc.,
as are other names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
Chapter 2: Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Walkthrough for Bulk Insert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Walkthrough for Bulk Insert With A Multipart Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Walkthrough for Bulk Upsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Chapter 4: Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Create a Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Upload Job Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Close or Abort a Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Delete a Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Get All Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Get Job Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Get Job Successful Record Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Get Job Failed Record Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Get Job Unprocessed Record Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
CHAPTER 1 Bulk API 2.0
In this chapter ... Bulk API 2.0 provides a simple interface for quickly loading large amounts of data into your Salesforce
org.
• Process for Creating
Bulk API 2.0 Requests
Bulk API 2.0 is available in API version 41.0 and later.
1
Bulk API 2.0 Process for Creating Bulk API 2.0 Requests
UploadComplete All job data has been uploaded and the job is ready to be processed. Salesforce has put the job in
the queue.
InProgress The job is being processed by Salesforce. This includes automatic optimized chunking of job data
and processing of job operations.
Aborted The job was canceled by the job creator, or by a user with the “Manage Data Integrations” permission.
Salesforce automatically chunks the data for your job into multiple internal batches to improve performance while processing the job.
Salesforce creates a separate batch for each 10,000 records in your job data, up to a daily maximum of 100 million records. If the limit is
exceeded while processing your job data, the remaining data isn’t processed.
For each internal batch, if Salesforce can’t process all the records in a batch in 10 minutes or less, the batch fails. Salesforce will re-try
failed batches up to a maximum of 10 retries. If the batch still can’t be processed after 10 retries, the entire job is moved to the Failed
state and remaining job data is not processed. Use the Failed Record Results and Unprocessed Record Records resources to determine
what records were not processed and what errors occurred.
2
CHAPTER 2 Examples
In this chapter ... Use the following examples to understand how to do tasks with Bulk API v2.0
3
Examples Walkthrough for Bulk Insert
2. Create a job.
To do any Bulk API 2.0 task, such as inserting or updating records, you first create a job. The job specifies the type of object, such as
Contact, that you're loading. The job also specifies the operation you're performing, such as insert or delete. Once you've created a
job, you can use the resulting job ID in Bulk API 2.0 requests to close or abort (cancel) the job.
a. In workbench, log in to your org.
b. Go to Utilities > REST Explorer. Use Bulk API 2.0 to create a job by issuing a POST request with the following details:
Request Headers:
Content-Type: application/json; charset=UTF-8
Accept: application/json
URI:
/services/data/vXX.X/jobs/ingest/
Request Body:
{
"object" : "Contact",
"contentType" : "CSV",
"operation" : "insert",
"lineEnding": "CRLF"
}
If you’re inserting an object other than Contact, replace “Contact” in the request body with your object name.
When you’re using workbench, set lineEnding to CRLF. If you’re using another tool, set it to match the line endings in
your data file.
You should get a response that includes the job ID, with a job state of Open. You'll use the job ID in Bulk API 2.0 calls in the next
steps. You’ll also use the URL in the contentUrl field of the response in the next step when you upload your data.
4
Examples Walkthrough for Bulk Insert
In workbench's REST Explorer, use Bulk API 2.0 to create a job data request with the CSV data. Issue a PUT request with the following
details:
Request Headers:
Content-Type: text/csv
Accept: application/json
URI:
Use the URI provided in the contentUrl field of the response from step 1. The URI has a format similar to the following example.
/services/data/vXX.X/jobs/ingest/JOB ID/batches/
Request Body:
(Content of your CSV file)
For the Request Body, copy and paste the CSV data into workbench's Request Body text field. With Bulk API 2.0, you can submit CSV
data that does not in total exceed 150 MB in size (after base64 encoding).
You should get a response that includes the job ID still in the Open state. You could cancel the job at this point by using a PATCH
to set the job state to Aborted.
URI:
/services/data/vXX.X/jobs/ingest/JOB ID/
Request Body:
{
"state" : "UploadComplete"
}
URI:
/services/data/vXX.X/jobs/ingest/JOB ID/
Once a job has been completed and is in the JobComplete state (or Failed state), you can get details of which job data
records were successfully processed by issuing a GET request with the following details:
5
Examples Walkthrough for Bulk Insert With A Multipart Request
Request Headers:
Content-Type: application/json; charset=UTF-8
Accept: text/csv
/services/data/vXX.X/jobs/ingest/JOB ID/successfulResults/
You get a response that contains CSV data, with each row containing a record ID and information on whether that record was
successfully processed or not. To get details on records that encountered an error during processing, use a GET request using the
failedResults resource instead of the successfulResults resource.
2. Create a job.
To do any Bulk API 2.0 task, such as inserting or updating records, you first create a job. The job specifies the type of object, such as
Contact, that you're loading. The job also specifies the operation you're performing, such as insert or delete. In this walkthrough
we’ll also include job data in the request that creates the job.
Once you've created a job, you can use the resulting job ID in Bulk API 2.0 requests to close or abort (cancel) the job.
a. In workbench, log in to your org.
b. Go to Utilities > REST Explorer. Use Bulk API 2.0 to create a new job and upload job data by issuing a POST request with the
following details:
Request Headers:
Content-Type: multipart/form-data; boundary=BOUNDARY
Accept: application/json
URI:
/services/data/vXX.X/jobs/ingest/
Request Body:
--BOUNDARY
Content-Type: application/json
6
Examples Walkthrough for Bulk Insert With A Multipart Request
{
"object":"Contact",
"contentType":"CSV",
"operation": "insert"
“lineEnding”: “CRLF”
}
--BOUNDARY
Content-Type: text/csv
Content-Disposition: form-data; name="content"; filename="content"
If you’re inserting an object other than Contact, replace “Contact” in the request body with your object name. In this example,
“BOUNDARY” is used to mark the request body boundaries between the job details and the job CSV data.
When you’re using workbench, set lineEnding to CRLF. If you’re using another tool, set it to match the line endings in
your data file.
You should get a response that includes the job ID, with a job state of UploadComplete. You'll use the job ID in Bulk API
2.0 calls in the next steps.
URI:
/services/data/vXX.X/jobs/ingest/JOB ID/
Once a job has been completed and is in the JobComplete state (or Failed state), you can get details of which job data
records were successfully processed by issuing a GET request with the following details:
Request Headers:
Content-Type: application/json; charset=UTF-8
Accept: text/csv
/services/data/vXX.X/jobs/ingest/JOB ID/successfulResults/
You'll get a response that contains CSV data, with each row containing a record ID and information on whether that record was
successfully processed or not. To get details on records that encountered an error during processing, use a GET request using the
failedResults resource instead of the successfulResults resource.
7
Examples Walkthrough for Bulk Upsert
3. Create a job.
To do any Bulk API 2.0 task, such as inserting or updating records, you first create a job. The job specifies the type of object, such as
Contact, that you're loading. The job also specifies the operation you're performing, such as insert or delete. Once you've created a
job, you can use the resulting job ID in Bulk API 2.0 requests to close or abort (cancel) the job.
a. In workbench, log in to your org.
b. Go to Utilities > REST Explorer. Use Bulk API 2.0 to create a new job by issuing a POST request with the following details:
Request Headers:
Content-Type: application/json; charset=UTF-8
Accept: application/json
URI:
/services/data/vXX.X/jobs/ingest/
Request Body:
{
"object" : "Contact",
"externalIdFieldName" : name of external id field
"contentType" : "CSV",
"operation" : "upsert"
}
Note that you need to provide the name of the external ID field when you create the upsert job, using the
externalIdFieldName property. If you're inserting an object other than Contact, replace “Contact” in the request body
with your object name.
You should get a response that includes the job ID, with a job state of Open. You'll use the job ID in Bulk API 2.0 calls in the next
steps. You’ll also use the URL in the contentUrl field of the response in the next step when you upload your data.
8
Examples Walkthrough for Bulk Upsert
URI:
Use the URI provided in the contentUrl field of the response from step 3. The URI will have a format similar to the following
example.
/services/data/vXX.X/jobs/ingest/JOB ID/batches/
Request Body:
(Content of your CSV file)
For the Request Body, copy and paste the CSV data into workbench's Request Body text field. With Bulk API 2.0, you can submit CSV
data that does not in total exceed 150MB in size (after base64 encoding).
You should get a response that includes the job ID still in the Open state. You could cancel the job at this point by using a PATCH
to set the job state to Aborted.
URI:
/services/data/vXX.X/jobs/ingest/JOB ID/
Request Body:
{
"state" : "UploadComplete"
}
9
Examples Walkthrough for Bulk Upsert
URI:
/services/data/vXX.X/jobs/ingest/JOB ID/
Once a job has been completed and is in the JobComplete state (or Failed state), you can get details of which job data
records were successfully processed by issuing a GET request with the following details:
Request Headers:
Content-Type: application/json; charset=UTF-8
Accept: text/csv
/services/data/vXX.X/jobs/ingest/JOB ID/successfulResults/
You'll get a response that contains CSV data, with each row containing a record ID and information on whether that record was
successfully processed or not. To determine which records were created and which records were updated during the upsert job,
examine the Created field of the successful results. To get details on records that encountered an error during processing, use a
GET request using the failedResults resource instead of the successfulResults resource.
10
CHAPTER 3 Data Files
In this chapter ... Use comma-separated value (CSV) data when submitting data rows for Bulk API 2.0 jobs. Bulk API 2.0
supports several formatting options with CSV data, such as supporting multiple field delimiter characters
• Prepare CSV Files and line ending characters.
• Valid Date Format in
Records
• Relationship Fields in
a Header Row
• Sample CSV Files
11
Data Files Prepare CSV Files
12
Data Files Relationship Fields in a Header Row
FirstName,LastName,ReportsTo.Email
Tom,Jones,buyer@salesforcesample.com
13
Data Files Sample CSV Files
2. The OwnerId field is not polymorphic and has a relationshipName of Owner. It refers to a user and the indexed Id field
uniquely identifies the parent record.
Subject,Priority,Status,Lead:Who.Email,Owner.Id
Test Bulk API polymorphic reference field,Normal,Not
Started,lead@salesforcesample.com,005D0000001AXYz
Warning: The ObjectType: portion of a field column header is only required for a polymorphic field. You get an error if you
omit this syntax for a polymorphic field. You also get an error if you include this syntax for a field that is not polymorphic.
Simple CSV
This example contains three Account records and specifies the Name, Description, and NumberOfEmployees fields for each record.
Name,Description,NumberOfEmployees
TestAccount1,Description of TestAccount1,30
TestAccount2,Another description,40
TestAccount3,Yet another description,50
A job that uses this CSV data might be defined with the following job properties.
{
"object" : "Account",
"contentType" : "CSV",
"operation" : "insert"
}
A job that uses this CSV data and specifies that carriage return + line feed is used as the line ending sequence would use the following
job properties.
{
"object" : "Contact",
"contentType" : "CSV",
"operation" : "insert",
"lineEnding" : "CRLF"
}
14
Data Files Sample CSV Files
A job that uses this CSV data and specifies that semicolon is used as the column delimiter would use the following job properties.
{
"object" : "Contact",
"contentType" : "CSV",
"operation" : "insert",
"columnDelimiter" : "SEMICOLON"
}
A job that uses this CSV data might be defined with the following job properties.
{
"object" : "Contact",
"contentType" : "CSV",
"operation" : "insert"
}
A job that uses this CSV data might be an upsert job defined with the following properties.
{
"object" : "Contact",
"externalIdFieldName" : "ExternalId__c",
"contentType" : "CSV",
15
Data Files Sample CSV Files
"operation" : "upsert"
}
16
CHAPTER 4 Reference
In this chapter ... The API reference for Bulk API 2.0 includes all the actions that you can perform with jobs.
• Create a Job
• Upload Job Data
• Close or Abort a Job
• Delete a Job
• Get All Jobs
• Get Job Info
• Get Job Successful
Record Results
• Get Job Failed
Record Results
• Get Job Unprocessed
Record Results
17
Reference Create a Job
Create a Job
Creates a job, which represents a bulk operation (and associated data) that is sent to Salesforce for asynchronous processing. Provide
job data via an Upload Job Data request, or as part of a multipart create job request.
URI
/services/data/vXX.X/jobs/ingest
Availability
This resource is available in API version 41.0 and later.
Formats
JSON
HTTP Method
POST
Authentication
Authorization: Bearer token
Parameters
None.
Request Body
contentType ContentType The content type for the job. The only valid value (and the Optional
default) is CSV.
externalIdFieldName string The external ID field in the object being updated. Only Required for
needed for upsert operations. Field values must also exist upsert
in CSV job data. operations
lineEnding LineEndingEnum The line ending used for CSV job data, marking the end of Optional
a data row. The default is LF. Valid values are:
• LF—linefeed character
• CRLF—carriage return character followed by a
linefeed character
18
Reference Create a Job
operation OperationEnum The processing operation for the job. Valid values are: Required
• insert
• delete
• update
• upsert
For multipart requests, the request body can also include CSV record data. See Usage Notes for more details.
Response Body
columnDelimiter ColumnDelimiterEnum The column delimiter used for CSV job data. Values include:
• BACKQUOTE—backquote character (`)
• CARET—caret character (^)
• COMMA—comma character (,) which is the default delimiter
• PIPE—pipe character (|)
• SEMICOLON—semicolon character (;)
• TAB—tab character
concurrencyMode ConcurrencyModeEnum For future use. How the request was processed. Currently only parallel
mode is supported. (When other modes are added, the mode will be
chosen automatically by the API and will not be user configurable.)
contentType ContentType The format of the data being processed. Only CSV is supported.
contentUrl URL The URL to use for Upload Job Data requests for this job. Only valid if the
job is in Open state.
createdDate dateTime The date and time in the UTC time zone when the job was created.
19
Reference Create a Job
object string The object type for the data being processed.
state JobStateEnum The current state of processing for the job. Values include:
• Open—The job has been created, and data can be added to the job.
• UploadComplete—No new data can be added to this job. You
can’t edit or save a closed job.
• Aborted—The job has been aborted. You can abort a job if you
created it or if you have the “Manage Data Integrations” permission.
• JobComplete—The job was processed by Salesforce.
• Failed—Some records in the job failed. Job data that was
successfully processed isn’t rolled back.
systemModstamp dateTime Date and time in the UTC time zone when the job finished.
Usage Notes
For small amounts of job data (20,000 characters or less), you can create a job and upload all the data for a job using a multipart
request. The following example request header and body uses a multipart format to contain both job information and job data.
Content-Type: multipart/form-data; boundary=BOUNDARY
--BOUNDARY
Content-Type: application/json
Content-Disposition: form-data; name="job"
{
"object":"Contact",
"contentType":"CSV",
"operation":"insert"
}
--BOUNDARY
Content-Type: text/csv
Content-Disposition: form-data; name="content"; filename="content"
20
Reference Upload Job Data
21
Reference Close or Abort a Job
Authentication
Authorization: Bearer token
Parameters
None.
Request Body
Response Body
columnDelimiter ColumnDelimiterEnum The column delimiter used for CSV job data. Values include:
• BACKQUOTE—backquote character (`)
• CARET—caret character (^)
• COMMA—comma character (,) which is the default delimiter
• PIPE—pipe character (|)
• SEMICOLON—semicolon character (;)
• TAB—tab character
concurrencyMode ConcurrencyModeEnum For future use. How the request was processed. Currently only parallel
mode is supported. (When other modes are added, the mode will be
chosen automatically by the API and will not be user configurable.)
contentType ContentType The format of the data being processed. Only CSV is supported.
contentUrl URL The URL to use for Upload Job Data requests for this job. Only valid if the
job is in Open state.
createdDate dateTime The date and time in the UTC time zone when the job was created.
22
Reference Delete a Job
object string The object type for the data being processed.
state JobStateEnum The current state of processing for the job. Values include:
• Open—The job has been created, and data can be added to the job.
• UploadComplete—No new data can be added to this job. You
can’t edit or save a closed job.
• Aborted—The job has been aborted. You can abort a job if you
created it or if you have the “Manage Data Integrations” permission.
• JobComplete—The job was processed by Salesforce.
• Failed—Some records in the job failed. Job data that was
successfully processed isn’t rolled back.
systemModstamp dateTime Date and time in the UTC time zone when the job finished.
Delete a Job
Deletes a job. To be deleted, a job must have a state of UploadComplete, JobComplete, Aborted, or Failed.
URI
/services/data/vXX.X/jobs/ingest/jobID
Availability
This resource is available in API version 41.0 and later.
Formats
JSON
HTTP Method
DELETE
Authentication
Authorization: Bearer token
Parameters
None.
Request Body
None required.
23
Reference Get All Jobs
Response Body
None. Returns a status code of 204 (No Content), which indicates that the job was successfully deleted.
Usage Notes
When a job is deleted, job data stored by Salesforce is also deleted and job metadata information is removed. The job will no longer
display in the Bulk Data Load Jobs page in Salesforce.
Parameter Description
isPkChunkingEnabled If set to true, filters jobs with PK chunking enabled.
queryLocator Use queryLocator with a locator value to get a specific set of job results. Get All Jobs
returns up to 1000 result rows per request, along with a nextRecordsUrl value that
contains the locator value used to get the next set of results.
Request Body
None required.
Response Body
24
Reference Get All Jobs
JobInfo
columnDelimiter ColumnDelimiterEnum The column delimiter used for CSV job data. Values include:
• BACKQUOTE—backquote character (`)
• CARET—caret character (^)
• COMMA—comma character (,) which is the default delimiter
• PIPE—pipe character (|)
• SEMICOLON—semicolon character (;)
• TAB—tab character
concurrencyMode ConcurrencyModeEnum For future use. How the request was processed. Currently only parallel
mode is supported. (When other modes are added, the mode will be
chosen automatically by the API and will not be user configurable.)
contentType ContentType The format of the data being processed. Only CSV is supported.
contentUrl URL The URL to use for Upload Job Data requests for this job. Only valid if
the job is in Open state.
createdById string The ID of the user who created the job. Create the batch with the same
user.
createdDate dateTime The date and time in the UTC time zone when the job was created.
lineEnding LineEndingEnum The line ending used for CSV job data. Values include:
• LF—linefeed character
• CRLF—carriage return character followed by a linefeed character
object string The object type for the data being processed.
25
Reference Get Job Info
state JobStateEnum The current state of processing for the job. Values include:
• Open—The job has been created, and data can be added to the
job.
• UploadComplete—No new data can be added to this job.
You can’t edit or save a closed job.
• Aborted—The job has been aborted. You can abort a job if you
created it or if you have the “Manage Data Integrations” permission.
• JobComplete—The job was processed by Salesforce.
• Failed—Some records in the job failed. Job data that was
successfully processed isn’t rolled back.
systemModstamp dateTime Date and time in the UTC time zone when the job finished.
26
Reference Get Job Info
apiVersion string The API version that the job was created in.
columnDelimiter ColumnDelimiterEnum The column delimiter used for CSV job data. Values include:
• BACKQUOTE—backquote character (`)
• CARET—caret character (^)
• COMMA—comma character (,) which is the default delimiter
• PIPE—pipe character (|)
• SEMICOLON—semicolon character (;)
• TAB—tab character
concurrencyMode ConcurrencyModeEnum For future use. How the request was processed. Currently only parallel
mode is supported. (When other modes are added, the mode will be
chosen automatically by the API and will not be user configurable.)
contentType ContentType The format of the data being processed. Only CSV is supported.
contentUrl URL The URL to use for Upload Job Data requests for this job. Only valid if
the job is in Open state.
createdDate dateTime The date and time in the UTC time zone when the job was created.
lineEnding LineEndingEnum The line ending used for CSV job data. Values include:
• LF—linefeed character
• CRLF—carriage return character followed by a linefeed character
numberRecordsFailed int The number of records that were not processed successfully in this
job.
object string The object type for the data being processed.
27
Reference Get Job Successful Record Results
retries int The number of times that Salesforce attempted to save the results of
an operation. The repeated attempts are due to a problem, such as a
lock contention.
state JobStateEnum The current state of processing for the job. Values include:
• Open: The job has been created, and job data can be uploaded
to the job.
• UploadComplete: All data for a job has been uploaded, and
the job is ready to be queued and processed. No new data can be
added to this job. You can’t edit or save a closed job.
• Aborted: The job has been aborted. You can abort a job if you
created it or if you have the “Manage Data Integrations” permission.
• JobComplete: The job was processed by Salesforce.
• Failed: Some records in the job failed. Job data that was
successfully processed isn’t rolled back.
systemModstamp dateTime Date and time in the UTC time zone when the job finished.
28
Reference Get Job Failed Record Results
Request Body
None required.
Response Body
The response body is a CSV file that all the records that the job successfully processed. Each row corresponds to a successfully
processed record and contains the following information.
Fields from the various Field data for the row that was provided in the original job data upload request.
original CSV
request data
Usage Notes
The order of records in the response is not guaranteed to match the ordering of records in the original job data.
sf__Id string ID of the record that had an error during processing, if applicable.
29
Reference Get Job Unprocessed Record Results
Usage Notes
The order of records in the response is not guaranteed to match the ordering of records in the original job data.
Usage Notes
The order of records in the response is not guaranteed to match the ordering of records in the original job data.
30