Query Objects in Marketing Cloud Next Using Bulk API 2.0

For complex queries, or queries that you expect to produce a large set of results, use Bulk API 2.0. Bulk API 2.0 executes queries asynchronously.

Prerequisite

Before you issue a query request, create a SOQL statement that specifies the data that you want to retrieve. Remove any line breaks from your SOQL statement. For more information, see Understand SOQL Queries.

note: Bulk queries don’t support the complete set of SOQL clauses and functions. For more information, see Create a Query Job in the Bulk API 2.0 and Bulk API Developer Guide.

Step 1: Create a Bulk Query Job

To create a bulk query job, issue a POST request to the /services/data/{apiVersion}/jobs/query endpoint. For more information about the properties of the request body, see Create a Query Job in the Bulk API 2.0 and Bulk API Developer Guide.

POST /services/data/{apiVersion}/jobs/query HTTP/1.1
Host: YOUR_SUBDOMAIN.my.salesforce.com
Content-Type: application/json
Authorization: Bearer YOUR_BEARER_TOKEN

{
  "operation" : "query",
  "query" : "SELECT Id, FirstName, LastName, MailingCountry FROM Contact WHERE MailingCountry = 'US'",
  "contentType" : "CSV",
  "columnDelimiter" : "COMMA",
  "lineEnding" : "LF"
}

If the request is successful, the response includes the job ID (id), which you use to check the status of the request and to retrieve the resulting data.

{
  "id": "7501Q00001CfAxxQAF",
  "operation": "query",
  "object": "Contact",
  "createdById": "0051Q00000booXXQAI",
  "createdDate": "2024-01-18T22:19:31.000+0000",
  "systemModstamp": "2024-01-18T22:19:31.000+0000",
  "state": "UploadComplete",
  "concurrencyMode": "Parallel",
  "contentType": "CSV",
  "apiVersion": 60.0,
  "lineEnding": "LF",
  "columnDelimiter": "COMMA"
}

Step 2: Check the Status of the Query

To check the status of a bulk query, issue a GET request to the /data/{apiVersion}/jobs/query/JOB_ID endpoint, replacing JOB_ID with the unique ID of the query job.

{
  "id": "7501Q00001CfAdyQAF",
  "operation": "query",
  "object": "Contact",
  "createdById": "0051Q00000bogY5QAI",
  "createdDate": "2024-01-18T22:25:23.000+0000",
  "systemModstamp": "2024-01-18T22:25:24.000+0000",
  "state": "JobComplete",
  "concurrencyMode": "Parallel",
  "contentType": "CSV",
  "apiVersion": 60.0,
  "jobType": "V2Query",
  "lineEnding": "LF",
  "columnDelimiter": "COMMA",
  "numberRecordsProcessed": 2,
  "retries": 0,
  "totalProcessingTime": 341,
  "isPkChunkingSupported": false
}

When the status of the query job is JobComplete, the query has finished running and your data is ready to retrieve.

Step 3: Retrieve the Results

To retrieve the query results, issue a GET request to the /data/{apiVersion}/jobs/query/JOB_ID/results endpoint.

If the request is successful, the response includes the resulting data in the requested format, as shown in this example.

"Id","FirstName","LastName","MailingCountry"
"0031Q00002joOXXQAM","Rachel","Rodriquez","US"
"0031Q00002joOXXQAU","Linda","Rosenberg","US"