Newer Version Available

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

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.

Note

Create a Job

  1. Create a file called create-job.xml containing the following text.
  2. Using a command-line window, execute the following 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/50.0/job

    instance 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 the following.

Add a Batch to the Job

  1. Create a file called query.txt to contain the SOQL query statement.
  2. Using a command-line window, execute the following 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/50.0/job/jobId/batch

    jobId is the job ID returned in the response to the job createion.

    Salesforce returns an XML response with data such as the following.

    When adding a batch to a bulk query job, the Content-Type in the header for the request must be either text/csv, application/xml, or application/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.

    Note

Check the Status of the Job and Batch

  1. Using a command-line window, execute the following cURL command to check the job status.

    curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/50.0/job/jobId

    Salesforce returns an XML response with data such as the following.

  2. Using a command-line window, execute the following cURL command to check the batch status.

    curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/50.0/job/jobId/batch/batchId

    batchId is the batch ID in the response to the batch creation.

    Salesforce returns an XML response with data such as the following.

Retrieve the Results

  1. Using the command-line window, execute the following cURL command to retrieve the batch result list.

    curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/50.0/job/jobId/batch/batchId/result

    Salesforce returns an XML response with data such as the following.

    If the batch required retries, there will be more than one <result> element in the output.

    Note

  2. Using the command-line window, execute the following cURL command to retrieve the results of the query.

    curl -H "X-SFDC-Session: sessionId" https://instance.salesforce.com/services/async/50.0/job/jobId/batch/batchId/result/resultId

    resultId is the result ID in the response to the batch result list request.

    Salesforce returns a CSV response with data such as the following.

Close the Job

  1. Create a file called close-job.xml containing the following text.
  2. Using a command-line window, execute the following 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/50.0/job/jobId

    Salesforce returns an XML response with data such as the following.