Sub topic 12
Exporting data with bulk query jobs
This chapter assumes you can already create a job, poll its state, and call the REST endpoints with an OAuth access token. A bulk query job uses the same job model as an ingest job, but you send no CSV. You send a SOQL query, and Salesforce writes the results for you to download.
How a query job differs from an ingest job
An ingest job needs an upload step and an UploadComplete step. A query job skips both. Salesforce starts processing as soon as you create the job.
| Step | Ingest job | Query job |
|---|---|---|
| Create | `POST /jobs/ingest` | `POST /jobs/query` |
| Upload data | `PUT .../batches` | Not needed |
| Start processing | Patch state to `UploadComplete` | Automatic |
| Get output | Successful, failed and unprocessed results | `GET .../results` |
Creating the job and fetching results
The request body takes an operation and a query. Use query for current records. Use queryAll to include deleted and archived records.
curl -X POST "$INSTANCE_URL/services/data/v62.0/jobs/query" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"operation":"query","query":"SELECT Id, Name FROM Account"}'The response contains the job id. Poll GET /jobs/query/$QUERY_JOB_ID until state is JobComplete. The other terminal states are Failed and Aborted. Then download the data:
curl -D - "$INSTANCE_URL/services/data/v62.0/jobs/query/$QUERY_JOB_ID/results?maxRecords=50000" \
-H "Authorization: Bearer $ACCESS_TOKEN"The -D - option prints the response headers before the CSV body. The first line of the body is a header row with field names. Each later line is one record.
The maxRecords parameter sets the page size. If the job has more rows than one page holds, the response includes a Sforce-Locator header. Pass its value as the locator parameter to fetch the next page. The next chapter covers this in detail.
Query restrictions
Bulk queries accept SOQL, but not every feature works. Check these before you debug an unexpected failure:
- Parent-to-child subqueries, such as
SELECT Id, (SELECT Id FROM Contacts) FROM Account, are not supported. Query the child object directly and filter on itsAccountId. - Child-to-parent fields, such as
Account.Nameon Contact, work normally. GROUP BY,OFFSETandTYPEOFare not supported.- Results are returned as CSV only. Date and time values come back in ISO 8601 format.
Common mistakes
- Requesting results before the state is
JobComplete. Poll first. - Assuming one results call returns everything. Always check for
Sforce-Locator. - Querying fields the integration user cannot see. Field-level security applies, so the job can fail or return fewer columns than expected.
Exercise
You created a query job and the first results call returned 50,000 rows, but your object has 120,000 matching records. How do you get the rest?
Show answer
Read the Sforce-Locator response header from the first results call. Call the results endpoint again with that value as the locator parameter, and repeat until the header is no longer present or its value is null.
Exercise
A teammate writes SELECT Id, (SELECT LastName FROM Contacts) FROM Account as a bulk query and the job fails. What is the cause and the fix?
Show answer
Bulk API 2.0 does not support parent-to-child subqueries. Run a separate query job on Contact, selecting LastName and AccountId, and join the two CSV files on the account id.