Sub topic 14
Limits and how Salesforce batches your data
This chapter assumes you can create an ingest job, upload CSV data and close the job with UploadComplete. It explains what Salesforce does with your data after that, and which limits you need to plan around.
Batching is automatic
Bulk API 2.0 has no batch objects in its REST interface. You upload one CSV, and Salesforce splits it into internal batches, up to 10,000 records each. Those batches are processed in parallel and may run in any order. The job state and record counts you poll are the only view you get of that work.
Two consequences follow:
- Row order in your file does not guarantee processing order.
- Records that touch the same parent can be processed in different batches at the same moment. This is the cause of lock contention, covered in the next chapter.
Limits to plan around
The values below are typical figures. Salesforce changes them between releases and by edition, so check the current "Bulk API and Bulk API 2.0 Limits" page for your org.
| Limit | What it means for you |
|---|---|
| Upload size per job | About 150 MB of CSV. Split larger files into several jobs. |
| Field and record size | A single field is limited to 32,000 characters, and a record to 400,000 characters. |
| Daily capacity | Bulk API 2.0 counts against a rolling 24-hour allowance of records and internal batches. |
| Result retention | Job results and query results are kept for about seven days. |
| Batch time | A batch that runs too long is requeued and retried a limited number of times, then fails. |
Slow batches usually come from triggers, flows, validation rules or sharing recalculation, not from the API. A load that works on 1,000 rows can still hit the time limit at full volume.
Splitting a large file
Each job holds one upload, so a file over the size limit must be cut into pieces. Split on row boundaries and repeat the header row in every piece. This example does that by size.
import csv
MAX_BYTES = 100 * 1024 * 1024 # stay well under the upload limit
def split_csv(path, prefix):
part, size, out, writer = 0, 0, None, None
with open(path, newline="", encoding="utf-8") as src:
reader = csv.reader(src)
header = next(reader)
for row in reader:
row_size = sum(len(v.encode("utf-8")) + 1 for v in row) + 1
if out is None or size + row_size > MAX_BYTES:
if out:
out.close()
part += 1
out = open(f"{prefix}_{part}.csv", "w", newline="", encoding="utf-8")
writer = csv.writer(out, lineterminator="\n")
writer.writerow(header)
size = 0
writer.writerow(row)
size += row_size
if out:
out.close()
return part
print(split_csv("contacts.csv", "contacts_part"), "files written")Create one job per piece. Run them one after another, or a few at a time, and keep an eye on the daily allowance.
Common errors
- Upload rejected as too large. The file exceeds the per-job size limit. Split it as shown above.
- Job fails near the start of the day's quota. Earlier jobs, including ones from other tools, used up the rolling allowance. Check your org's limits.
- Batches fail with timeouts. Reduce automation on the target object during the load, or load smaller jobs.
- Results are gone. You waited past the retention period. Download results as soon as the job finishes.
Exercise
You have a 400 MB CSV of Contacts. You upload it as a single job and the request is rejected. What should you do, and why can't you control which records land in which internal batch?
Show answer
Split the file on row boundaries into pieces under the per-job size limit, repeat the header in each piece, and create one ingest job per piece. You can't control internal batches because Salesforce splits each job's data automatically, in chunks of up to 10,000 records, and processes them in parallel and in no guaranteed order.