Step 6: Query Jobs

This Bulk API 2.0 example shows you how to create a query job, monitor its progress, and get the job results.
  1. Create the job.

    URI

    /services/data/v63.0/jobs/query

    Example of creating a bulk query job

    curl https://MyDomainName.my.salesforce.com/services/data/v63.0/jobs/query -H 'Content-Type: application/json' -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "X-PrettyPrint:1" --data-raw '{ "operation" : "query", "query" : "SELECT Id, Name FROM Account" } ' -X POST

    The response includes the job id and shows the job’s state as UploadComplete. (You use the job id to monitor the job or get its results.)

    Example response body

    {
      "id" : "7986gEXAMPLE4X2OPT",
      "operation" : "query",
      "object" : "Account",
      "createdById" : "0055fEXAMPLEtG4AAM",
      "createdDate" : "2022-01-02T17:38:59.000+0000",
      "systemModstamp" : "2022-01-02T17:38:59.000+0000",
      "state" : "UploadComplete",
      "concurrencyMode" : "Parallel",
      "contentType" : "CSV",
      "apiVersion" : 63.0,
      "lineEnding" : "LF",
      "columnDelimiter" : "COMMA"
    }
  2. Monitor the job’s state using the returned job id.

    URI

    /services/data/v63.0/jobs/query/queryJobId

    Example of monitoring the state of the query job

    curl https://MyDomainName.my.salesforce.com/services/data/v63.0/jobs/query/7986gEXAMPLE4X2OPT -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "X-PrettyPrint:1" -X GET

    Example response body

    The response shows the current state of the job. Repeat this step until the state is JobComplete.

    {
      "id" : "7986gEXAMPLE4X2OPT",
      "operation" : "query",
      "object" : "Account",
      "createdById" : "0055fEXAMPLEtG4AAM",
      "createdDate" : "2022-01-02T17:38:59.000+0000",
      "systemModstamp" : "2022-01-02T17:39:00.000+0000",
      "state" : "JobComplete",
      "concurrencyMode" : "Parallel",
      "contentType" : "CSV",
      "apiVersion" : 63.0,
      "jobType" : "V2Query",
      "lineEnding" : "LF",
      "columnDelimiter" : "COMMA",
      "numberRecordsProcessed" : 28,
      "retries" : 0,
      "totalProcessingTime" : 153,
      "isPkChunkingSupported": true
    }
  3. Get the results of the job.

    URI

    /services/data/v63.0/jobs/query/queryJobId/results

    Example of getting the results of the job

    curl https://MyDomainName.my.salesforce.com/services/data/v63.0/jobs/query/7986gEXAMPLE4X2OPT/results/ -H 'Authorization: Bearer 00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE' -H "Content-Type: application/json; charset=UTF-8" -H "Accept: text/csv" -H "X-PrettyPrint:1" -X GET

    The response shows the results of the SOQL query when you created the query job.

    Example response body

    "Id","Name"
    "0015f00000BCvReAAL","Sample Account for Entitlements"
    "0015f00000BFjNuAAL","University of The Terrific"
    "0015f00000C6beUAAR","Edge Communications"
    "0015f00000C6beVAAR","Burlington Textiles Corp of America"
    "0015f00000C6beWAAR","Pyramid Construction Inc."
    "0015f00000C6beXAAR","Dickenson plc"
    "0015f00000C6beYAAR","Grand Hotels & Resorts Ltd"
    "0015f00000C6beZAAR","United Oil & Gas Corp."
    "0015f00000C6beaAAB","Express Logistics and Transport"
    "0015f00000C6bebAAB","University of Arizona"
    "0015f00000C6becAAB","United Oil & Gas, UK"
    "0015f00000C6bedAAB","United Oil & Gas, Singapore"
    "0015f00000C6beeAAB","GenePoint"

    This example returns a small result set, and it’s easy to see the complete results. Queries that return larger results spread them across a sequence of result sets. To see the other result sets, use the locator to fetch the next set of results. For more information, see Get Results for a Query Job.