Sub topic 13
Paging through query results
This chapter assumes you can create a bulk query job and wait for it to reach JobComplete. It also assumes you can send authenticated HTTP requests from curl or Python.
Why results come in pages
A bulk query job can produce millions of rows. Salesforce does not return them in one response. You ask for the results in sets, and each set is one HTTP response. The maxRecords parameter sets how many records each set holds. Salesforce tells you where to continue with a locator, an opaque string that identifies the next set.
The locator in the response headers
The first request is the one from the bulk query chapter. It has no locator parameter:
curl -D - "$INSTANCE_URL/services/data/v62.0/jobs/query/$QUERY_JOB_ID/results?maxRecords=50000" \
-H "Authorization: Bearer $ACCESS_TOKEN"The body is CSV. Two response headers matter:
| Header | Meaning |
|---|---|
| `Sforce-Locator` | The locator for the next set. The literal text `null` means there are no more sets. |
| `Sforce-NumberOfRecords` | How many records this set contains. |
To fetch the next set, repeat the call and add the locator:
curl "$INSTANCE_URL/services/data/v62.0/jobs/query/$QUERY_JOB_ID/results?maxRecords=50000&locator=$LOCATOR" \
-H "Authorization: Bearer $ACCESS_TOKEN"A complete paging loop
This script writes every set into one file. Each set starts with its own CSV header row, so the script drops that row from every set after the first. It assumes the default LF line ending.
import os
import requests
instance = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
job_id = os.environ["QUERY_JOB_ID"]
url = f"{instance}/services/data/v62.0/jobs/query/{job_id}/results"
auth = {"Authorization": f"Bearer {token}"}
locator = None
first_page = True
with open("accounts.csv", "wb") as out:
while True:
params = {"maxRecords": 50000}
if locator:
params["locator"] = locator
resp = requests.get(url, headers=auth, params=params)
resp.raise_for_status()
body = resp.content
if not first_page:
body = body.split(b"\n", 1)[1] if b"\n" in body else b""
out.write(body)
first_page = False
locator = resp.headers.get("Sforce-Locator")
if not locator or locator == "null":
breakCommon errors
- Treating
nullas a real locator. The header value is the stringnull. If you sendlocator=null, your loop never ends or the call fails. Compare the value to"null"before you continue. - Duplicate header rows. Each set repeats the column names. If you concatenate sets without removing them, your data contains rows of column names.
- Querying before the job finishes. Results are only available when the job state is
JobComplete. Poll the job first. - Losing the locator. Keep each locator until its set is safely saved. If a download fails midway, repeat the same request with the same locator instead of starting over.
Exercise
A results response has the header Sforce-Locator: null. What does this tell you, and what should your loop do?
Show answer
It means the response is the last set of results. The loop should save that body and stop, without sending another request.