Walk Through a Bulk Query Sample
This code sample uses cURL to query several account records.
Before you begin building an integration or other client application:
- Install your development platform according to its product documentation.
- Read through all the steps before creating the test client application. Also review the rest of this document to familiarize yourself with terms and concepts.
-
Create a file called create-job.xml containing this text.
-
Using a command-line window, execute this cURL command to create a job.
curl -H "X-SFDC-Session: sessionId" -H "Content-Type: application/xml; charset=UTF-8" -d @create-job.xml https://instance.salesforce.com/services/async/68.0/jobinstance is the portion of the
<serverUrl>element and sessionId is the<sessionId>element that you noted in the login response.Salesforce returns an XML response with data such as this.
-
Create a file called query.txt to contain the SOQL query statement.
-
Using a command-line window, execute this cURL command to add a batch to the job:
curl -d @query.txt -H "X-SFDC-Session: sessionId" -H "Content-Type: text/csv; charset=UTF-8" https://instance.salesforce.com/services/async/68.0/job/jobId/batchjobId is the job ID returned in the response to the job creation.
Salesforce returns an XML response with data such as this.
When you add a batch to a bulk query job, the Content-Type in the header for the request must be
text/csv,application/xml, orapplication/json, depending on the content type specified when the job was created. The actual SOQL statement supplied for the batch is in plain text format.
-
Create a file called close-job.xml containing this text.
-
Using a command-line window, execute this cURL command to close the job.
curl -H "X-SFDC-Session: sessionId" -H "Content-Type: text/csv; charset=UTF-8" -d @close-job.xml https://instance.salesforce.com/services/async/68.0/job/jobIdSalesforce returns an XML response with data such as this.
-
Using a command-line window, execute this cURL command to check the job status.
curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/68.0/job/jobIdSalesforce returns an XML response with data such as this.
-
Using a command-line window, execute this cURL command to check the batch status.
curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/68.0/job/jobId/batch/batchIdbatchId is the batch ID in the response to the batch creation.
Salesforce returns an XML response with data such as this.
-
Using the command-line window, execute this cURL command to retrieve the batch result list.
curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/68.0/job/jobId/batch/batchId/resultSalesforce returns an XML response with data such as this.
If the batch required retries, there will be more than one
<result>element in the output. -
Using the command-line window, execute this cURL command to retrieve the results of the query.
curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/68.0/job/jobId/batch/batchId/result/resultIdresultId is the result ID in the response to the batch result list request.
Salesforce returns a CSV response with data such as this.
See Also