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 this text.
    1<?xml version="1.0" encoding="UTF-8"?>
    2<jobInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <operation>query</operation>
    5  <object>Account</object>
    6  <concurrencyMode>Parallel</concurrencyMode>
    7  <contentType>CSV</contentType>
    8</jobInfo>
  2. 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/53.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 this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<jobInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <id>750x000000009tvAAA</id>
    5  <operation>query</operation>
    6  <object>Account</object>
    7  <createdById>005x0000001WR0lAAG</createdById>
    8  <createdDate>2016-01-10T00:53:19.000Z</createdDate>
    9  <systemModstamp>2016-01-10T00:53:19.000Z</systemModstamp>
    10  <state>Open</state>
    11  <concurrencyMode>Parallel</concurrencyMode>
    12  <contentType>CSV</contentType>
    13  <numberBatchesQueued>0</numberBatchesQueued>
    14  <numberBatchesInProgress>0</numberBatchesInProgress>
    15  <numberBatchesCompleted>0</numberBatchesCompleted>
    16  <numberBatchesFailed>0</numberBatchesFailed>
    17  <numberBatchesTotal>0</numberBatchesTotal>
    18  <numberRecordsProcessed>0</numberRecordsProcessed>
    19  <numberRetries>0</numberRetries>
    20  <apiVersion>36.0</apiVersion>
    21  <numberRecordsFailed>0</numberRecordsFailed>
    22  <totalProcessingTime>0</totalProcessingTime>
    23  <apiActiveProcessingTime>0</apiActiveProcessingTime>
    24  <apexProcessingTime>0</apexProcessingTime>
    25</jobInfo>

Add a Batch to the Job

  1. Create a file called query.txt to contain the SOQL query statement.
    1SELECT Id, Name FROM Account LIMIT 10
  2. 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/53.0/job/jobId/batch

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

    Salesforce returns an XML response with data such as this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<batchInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <id>751x000000009vwAAA</id>
    5  <jobId>750x000000009tvAAA</jobId>
    6  <state>Queued</state>
    7  <createdDate>2016-01-10T00:59:47.000Z</createdDate>
    8  <systemModstamp>2016-01-10T00:59:47.000Z</systemModstamp>
    9  <numberRecordsProcessed>0</numberRecordsProcessed>
    10  <numberRecordsFailed>0</numberRecordsFailed>
    11  <totalProcessingTime>0</totalProcessingTime>
    12  <apiActiveProcessingTime>0</apiActiveProcessingTime>
    13  <apexProcessingTime>0</apexProcessingTime>
    14</batchInfo>

    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, 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 this cURL command to check the job status.

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

    Salesforce returns an XML response with data such as this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<jobInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <id>750x000000009tvAAA</id>
    5  <operation>query</operation>
    6  <object>Account</object>
    7  <createdById>005x0000001WR0lAAG</createdById>
    8  <createdDate>2016-01-10T00:53:19.000Z</createdDate>
    9  <systemModstamp>2016-01-10T00:53:19.000Z</systemModstamp>
    10  <state>Open</state>
    11  <concurrencyMode>Parallel</concurrencyMode>
    12  <contentType>CSV</contentType>
    13  <numberBatchesQueued>0</numberBatchesQueued>
    14  <numberBatchesInProgress>0</numberBatchesInProgress>
    15  <numberBatchesCompleted>1</numberBatchesCompleted>
    16  <numberBatchesFailed>0</numberBatchesFailed>
    17  <numberBatchesTotal>1</numberBatchesTotal>
    18  <numberRecordsProcessed>10</numberRecordsProcessed>
    19  <numberRetries>0</numberRetries>
    20  <apiVersion>36.0</apiVersion>
    21  <numberRecordsFailed>0</numberRecordsFailed>
    22  <totalProcessingTime>0</totalProcessingTime>
    23  <apiActiveProcessingTime>0</apiActiveProcessingTime>
    24  <apexProcessingTime>0</apexProcessingTime>
    25</jobInfo>
  2. 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/53.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 this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<batchInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <id>751x000000009vwAAA</id>
    5  <jobId>750x000000009tvAAA</jobId>
    6  <state>Completed</state>
    7  <createdDate>2016-01-10T00:59:47.000Z</createdDate>
    8  <systemModstamp>2016-01-10T01:00:19.000Z</systemModstamp>
    9  <numberRecordsProcessed>10</numberRecordsProcessed>
    10  <numberRecordsFailed>0</numberRecordsFailed>
    11  <totalProcessingTime>0</totalProcessingTime>
    12  <apiActiveProcessingTime>0</apiActiveProcessingTime>
    13  <apexProcessingTime>0</apexProcessingTime>
    14</batchInfo>

Retrieve the Results

  1. 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/53.0/job/jobId/batch/batchId/result

    Salesforce returns an XML response with data such as this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<result-list xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    3  <result>752x00000004CJE</result>
    4</result-list>

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

    Note

  2. 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/53.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 this.

    1"Id","Name"
    2"001x000xxx4TU4JAAW","name161268--1296595660659"
    3"001x000xxx4TU4KAAW","name161269--1296595660659"
    4"001x000xxx4TU4LAAW","name161270--1296595660659"
    5"001x000xxx4TU4MAAW","name161271--1296595660659"
    6"001x000xxx4TU4NAAW","name161272--1296595660659"
    7"001x000xxx4TU4OAAW","name161273--1296595660659"
    8"001x000xxx4TU4PAAW","name161274--1296595660659"
    9"001x000xxx4TU4QAAW","name161275--1296595660659"
    10"001x000xxx4TU4RAAW","name161276--1296595660659"
    11"001x000xxx4TU4SAAW","name161277--1296595660659"

Close the Job

  1. Create a file called close-job.xml containing this text.
    1<?xml version="1.0" encoding="UTF-8"?>
    2<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    3  <state>Closed</state>
    4</jobInfo>
  2. 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/53.0/job/jobId

    Salesforce returns an XML response with data such as this.

    1<?xml version="1.0" encoding="UTF-8"?>
    2<jobInfo
    3    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    4  <id>750x000000009tvAAA</id>
    5  <operation>query</operation>
    6  <object>Account</object>
    7  <createdById>005x0000001WR0lAAG</createdById>
    8  <createdDate>2016-01-10T00:53:19.000Z</createdDate>
    9  <systemModstamp>2016-01-10T00:53:19.000Z</systemModstamp>
    10  <state>Closed</state>
    11  <concurrencyMode>Parallel</concurrencyMode>
    12  <contentType>CSV</contentType>
    13  <numberBatchesQueued>0</numberBatchesQueued>
    14  <numberBatchesInProgress>0</numberBatchesInProgress>
    15  <numberBatchesCompleted>1</numberBatchesCompleted>
    16  <numberBatchesFailed>0</numberBatchesFailed>
    17  <numberBatchesTotal>1</numberBatchesTotal>
    18  <numberRecordsProcessed>10</numberRecordsProcessed>
    19  <numberRetries>0</numberRetries>
    20  <apiVersion>36.0</apiVersion>
    21  <numberRecordsFailed>0</numberRecordsFailed>
    22  <totalProcessingTime>0</totalProcessingTime>
    23  <apiActiveProcessingTime>0</apiActiveProcessingTime>
    24  <apexProcessingTime>0</apexProcessingTime>
    25</jobInfo>