Bulk API 2.0¶
sf.bulk loads and reads large numbers of records with Bulk API 2.0 jobs. Use it for
thousands to millions of records, where sObject Collections would need too many calls.
import even_simpler_salesforce as esf
auth = esf.ClientCredentials(
domain="https://acme.my.salesforce.com", client_id="3MVG9...", client_secret="..."
)
sf = esf.AsyncSalesforce(auth)
rows = [{"LastName": f"Lead {n}", "Company": "Acme"} for n in range(1000)]
job = await sf.bulk.ingest("Lead", "insert", rows=rows)
info = await job.wait()
print(info.state, info.records_processed, info.records_failed) # JobComplete 1000 0
A job runs in Salesforce, not in your process. ingest() creates the job, uploads the data and
closes the upload. wait() then polls until the job is finished.
Load records¶
Pass the rows as dicts, as the path of a CSV file, or as CSV bytes. The operation is
"insert", "update", "upsert", "delete" or "hardDelete".
with open("leads.csv", "w", encoding="utf-8", newline="") as f: # keep LF line endings
f.write("External_Id__c,LastName,Company\next-1,Doe,Acme\next-2,Roe,Globex\n")
job = await sf.bulk.ingest("Lead", "upsert", external_id_field="External_Id__c", rows="leads.csv")
await job.wait()
await sf.bulk.ingest("Lead", "update", rows=b"Id,Status\n00Q8d0000000009AAA,Working\n")
The client streams the upload. It never reads a whole file into memory. Rows from an iterable go to a temporary file first, so a generator of a million rows is fine.
A CSV file must be UTF-8. A byte order mark at the start is removed: Salesforce would read it
as part of the first field name and fail the job. If your file uses CRLF line endings or another delimiter, say so with
line_ending="CRLF" or column_delimiter="TAB". The client checks the header row before it
sends anything.
Empty values and null¶
In Bulk CSV, an empty cell leaves the field unchanged and #N/A sets it to null. For dict rows
the client writes the cells for you.
| In the dict | In the CSV | On update | On insert |
|---|---|---|---|
"Description": None or "" |
#N/A |
sets the field to null | the field's default, else null |
| key left out | empty cell | leaves the field unchanged | the field's default, else null |
"Description": "#N/A" |
raises ValueError, because the meaning is unclear |
An upsert that creates a record behaves like an insert. A field with a default value, such as
Lead.Status, gets its default from both an empty cell and #N/A.
The keys of the first row are the columns. A later row may leave a key out, but it can't add one.
When rows set different fields, pass columns= with every field name. Each row then leaves out
the fields it doesn't set:
await sf.bulk.ingest(
"Lead",
"insert",
rows=[
{"LastName": "Doe", "Company": "Acme"},
{"LastName": "Roe", "Company": "Globex", "Phone": "+91 98765 43210"},
],
columns=["LastName", "Company", "Phone"],
)
await sf.bulk.ingest(
"Lead",
"update",
rows=[
{"Id": "00Q8d0000000009AAA", "Phone": None}, # clears Phone
{"Id": "00Q8d0000000010AAA"}, # changes nothing
],
)
Wait for a job¶
wait() polls the job status. By default it waits 2 seconds, then doubles the wait up to 30
seconds. It gives up after timeout seconds, 3600 by default.
job = await sf.bulk.ingest("Lead", "insert", rows=rows)
info = await job.wait(poll=esf.Backoff(start=2, max=30), timeout=3600)
print(info.total_processing_ms)
If the job ends Failed or Aborted, wait() raises esf.BulkJobFailed. If the time runs out,
it raises esf.BulkJobTimeout. The job keeps running in Salesforce, so you can call wait()
again.
job.info holds the status from the latest call. Call refresh() to get it again without
waiting.
To show progress, add an observer for the sf.bulk.job event. The
client emits it when the job state changes, and on each poll where records_processed or
records_failed changed.
Read the results¶
A job that ends JobComplete can still have rows that failed. Read them after wait().
async for row in job.successful_results():
print(row["sf__Id"], row["sf__Created"])
async for failure in job.failed_results():
print(failure.error.error_code, failure.error.message, failure.fields)
Each failed row is an esf.BulkFailure. Its error is an esf.ErrorDetail with the status code,
the message and the fields that Salesforce reported. fields holds the columns you uploaded.
If a job failed or was aborted, unprocessed_records() returns the rows that Salesforce didn't
process. Call abort() to stop a job that is still running.
Query records¶
sf.bulk.query() runs a query job, waits for it and yields every row. Rows are dicts of
strings. An empty value is None, and a parent field keeps its dotted name.
async for row in sf.bulk.query("SELECT Id, LastName, Status FROM Lead WHERE Status = {}", "Open"):
print(row["Id"], row["LastName"])
{} placeholders work as in sf.query(). Pass include_deleted=True to include deleted and
archived records, like queryAll.
The client reads each result page as it arrives, so memory stays flat for any result size. With
parallel_downloads=4 it downloads up to four pages at the same time. The rows still come in
order.
Run a query in a worker¶
sf.bulk.query() waits and reads in one call, so the job id stays inside it. For an export
that a worker runs, use start_query(). It creates the job and returns at once. Store
job.id, then open the job again with query_job(), in the same process or another one.
job = await sf.bulk.start_query("SELECT Id, Name FROM Lead WHERE Status = {}", "Open")
job_id = job.id # store it, for example in your queue message
# Later, in the worker:
job = await sf.bulk.query_job(job_id)
await job.wait()
async for row in job.rows(parallel_downloads=4):
print(row["Id"], row["Name"])
rows() and pages() read the results of a job that is JobComplete. If the job isn't
finished, they raise esf.BulkJobNotComplete, so call wait() first. If the job failed or
was aborted, they raise esf.BulkJobFailed. Salesforce keeps the results for 7 days after the
job completes, then deletes the job. After that, query_job() raises esf.NotFound.
An ingest job can be opened again too. Pass all of its ids: a large upload runs as several
Salesforce jobs.
job = await sf.bulk.ingest("Lead", "insert", rows=[{"LastName": "Doe", "Company": "Acme"}])
saved_ids = list(job.ids)
job = await sf.bulk.ingest_job(saved_ids)
info = await job.wait()
print(info.records_processed)
Resume a download¶
pages() yields the results one page at a time. After you handle a page, save its
next_locator. If the worker restarts, open the job and pass the saved locator. The download
goes on from the next page.
checkpoint: dict[str, str | None] = {"job_id": job_id, "locator": None}
job = await sf.bulk.query_job(checkpoint["job_id"])
async for page in job.pages(locator=checkpoint["locator"], max_records=50_000):
async for row in page.rows():
print(row["Id"])
checkpoint["locator"] = page.next_locator # None after the last page
The client downloads each page to a temporary file before it yields the page. page.size is
the number of rows that Salesforce reported for it. Read the rows before you ask for the next
page: the client deletes the file when the iteration moves on. With max_records, pages are
smaller, so less is downloaded again after a restart. A locator stays valid as long as the job
exists.
Compound fields¶
Bulk queries can't read compound address and geolocation fields, such as BillingAddress.
expand_compound_fields() replaces them with their component fields.
fields = await sf.bulk.expand_compound_fields("Account", ["Id", "Name", "BillingAddress"])
print(fields) # ['Id', 'Name', 'BillingStreet', 'BillingCity', ...]
It reads the describe of the object, from the describe cache when it is fresh. Salesforce rejects
a query that selects a compound field when the client creates the job, with esf.RequestError
(400 API_ERROR, "Selecting compound data not supported in Bulk Query"). The error's hint
names the component fields when the describe is cached.
All fields¶
Query jobs don't accept FIELDS(ALL) or FIELDS(CUSTOM). If the query uses one of them,
sf.bulk.query() raises ValueError before it creates a job. FIELDS(STANDARD) works on
objects without compound fields, such as Campaign. On objects with compound fields, such as
Account and Lead, Salesforce rejects it like a compound field. Build the field list from the
describe instead:
describe = await sf.sobject("Account").describe()
names = await sf.bulk.expand_compound_fields("Account", [f["name"] for f in describe["fields"]])
async for row in sf.bulk.query(f"SELECT {', '.join(names)} FROM Account"):
pass # every field the user can read, compound fields as components
See Create a Query Job.
Methods¶
| Method | Returns | Description |
|---|---|---|
sf.bulk.ingest(sobject, operation, rows=...) |
ingest job | create a job, upload the data, close the upload |
job.wait(poll=, timeout=) |
BulkJobInfo |
poll until the job is finished |
job.refresh() |
BulkJobInfo |
get the status once |
job.abort() |
BulkJobInfo |
abort the job |
job.successful_results() |
rows | saved rows with sf__Id and sf__Created |
job.failed_results() |
BulkFailure items |
rejected rows with the parsed error |
job.unprocessed_records() |
rows | rows that Salesforce didn't process |
sf.bulk.query(soql, include_deleted=, parallel_downloads=) |
rows | run a query job and stream the rows |
sf.bulk.start_query(soql, include_deleted=) |
query job | create a query job without waiting |
sf.bulk.query_job(job_id) |
query job | open a query job by its id |
sf.bulk.ingest_job(job_ids) |
ingest job | open an ingest job by its ids |
job.rows(parallel_downloads=) |
rows | stream the rows of a complete query job |
job.pages(locator=, max_records=) |
pages | read a complete query job page by page |
page.rows() |
rows | the rows of one page |
sf.bulk.expand_compound_fields(sobject, fields) |
list[str] |
swap compound fields for their components |
Errors¶
| Exception | When |
|---|---|
esf.BulkJobFailed |
the job ended Failed or Aborted; .job_id, .state, .error_message |
esf.BulkJobTimeout |
wait() ran out of time; the job keeps running |
esf.BulkJobNotComplete |
rows() or pages() on a query job that isn't JobComplete; call wait() first |
esf.NotFound |
query_job() or ingest_job() with an unknown id, the id of the other job kind, or a job that Salesforce deleted |
esf.UnexpectedResponse |
Salesforce reported a job state that the client doesn't know |
ValueError |
invalid arguments or data, raised before anything is sent |
Testing¶
FakeSalesforce supports bulk. An ingest job applies its rows to the fake's records at once and
is JobComplete. A query returns the rows you scripted with on_query(), as strings. The fake
keeps its jobs, so query_job() and ingest_job() return them by id. In pages(), the fake
uses the row offset as the locator.
from even_simpler_salesforce.testing import FakeSalesforce
def test_import(esf_fake: FakeSalesforce) -> None:
job = esf_fake.bulk.ingest("Lead", "insert", rows=[{"LastName": "Doe", "Company": "Acme"}])
assert job.wait().records_processed == 1
assert esf_fake.records("Lead")[0]["LastName"] == "Doe"
How it works¶
- Salesforce accepts at most 150 MB of base64 data per upload, which is about 100 MB of CSV. If
your data is larger, the client splits it over several jobs. Each job gets the header row and
only whole records. The job object stands for all of them:
job.idslists them, and the results come from each job in turn. - If an upload fails, the client aborts that job and raises the error. Jobs it uploaded before keep running, and the error message names them.
- Results must be read with the API version that created the job, or Salesforce returns
409. The job keeps that version for every later call. A job opened withquery_job()oringest_job()takes the version from the job's status. - The client sends uploads as
text/csv; charset=UTF-8. It reads result files as UTF-8, whatever charset the response names. - An upload over 64 KiB is sent gzipped, with chunked transfer encoding. The 100 MB split applies to the CSV before compression.
- Parallel downloads use the
resultPagesresource, which gives up to five page URLs at a time. Values ofparallel_downloadsabove 5 don't download more. The client reads both response shapes:resultPageswithresultUrl(API 63.0 and later) andresultChunkswithresultLink(API 58.0 to 62.0). - Every change of job state or progress emits an
sf.bulk.jobevent.