Sub topic 11
Handling failures and resubmitting records
This chapter assumes you can create an ingest job, upload CSV data, close the job, and poll it until it reaches a terminal state. It also assumes you know how the three result endpoints work. Here you decide what to do when a job does not fully succeed.
Two kinds of failure
A load can fail at two levels, and each needs a different response.
- Job-level failure. The job ends in the
FailedorAbortedstate. Salesforce rejected the whole job, for example because of a malformed CSV, an unknown field name, or a bad header. Check theerrorMessagefield in the job info response. - Record-level failure. The job ends in
JobComplete, but some rows did not save. Validation rules, required fields, duplicate rules, and triggers cause these.JobCompletedoes not mean every row succeeded.
Always check numberRecordsFailed after a job completes. A job can finish with a large number of failed rows.
Reading the failed rows
Request failedResults/ for the job. Each row has the original columns plus two extra ones.
sf__Idis empty for an insert that failed. It holds the record ID for an update or upsert.sf__Errorholds the reason, such asREQUIRED_FIELD_MISSING:Required fields are missing: [LastName]:LastName --.
Group the rows by the error code before you change anything. Many failures often share one cause, and one fix then covers them all.
Choosing what to resubmit
Not every failure should be retried. Retrying a row that failed validation gives the same error.
| Error type | Example | Action |
|---|---|---|
| Bad data | `REQUIRED_FIELD_MISSING`, `INVALID_EMAIL_ADDRESS` | Fix the data, then resubmit |
| Transient | `UNABLE_TO_LOCK_ROW` | Resubmit unchanged |
| Duplicate | `DUPLICATE_VALUE` | Switch to upsert, or remove the row |
| Not attempted | Rows in `unprocessedrecords/` | Resubmit unchanged |
UNABLE_TO_LOCK_ROW means another process held a lock on a related record. It usually clears on its own, so retry after a short wait.
Building the retry file
The failed results file cannot be uploaded as it is, because it contains the sf__Id and sf__Error columns. Remove them first. The script below keeps only the rows with a retryable error and writes a clean CSV.
import csv
RETRYABLE = ("UNABLE_TO_LOCK_ROW",)
with open("failed.csv", newline="", encoding="utf-8") as src:
reader = csv.DictReader(src)
columns = [c for c in reader.fieldnames if c not in ("sf__Id", "sf__Error")]
rows = [r for r in reader if r["sf__Error"].startswith(RETRYABLE)]
with open("retry.csv", "w", newline="", encoding="utf-8") as out:
writer = csv.DictWriter(out, fieldnames=columns, extrasaction="ignore", lineterminator="\n")
writer.writeheader()
writer.writerows(rows)
print(f"{len(rows)} rows queued for retry")Upload retry.csv to a new job with the same object and operation. A job cannot be reopened after it closes, so each retry is a new job. Set lineEnding to LF to match the file this script writes.
The retry loop
Keep the loop bounded. Cap the number of attempts, for example at three, and wait longer between them. Rows still failing after the cap need a person to look at them.
Idempotency
A retry must not create duplicates. Insert jobs are not idempotent: if you resubmit rows that actually saved, you get two records. Resubmit only rows from failedResults/ and unprocessedrecords/, never the original file. For loads you may repeat, use upsert with an external ID so a second run updates existing records.
Exercise
A job ends in JobComplete with numberRecordsFailed of 40. All 40 rows show REQUIREDFIELDMISSING for LastName. Should you resubmit the failed rows unchanged, and why or why not?
Show answer
No. This is a data error, so the same rows fail again with the same error. Fill in LastName for those rows, remove the sf_Id and sf_Error columns, and submit them in a new job.