Sub topic 9
Reading successful, failed and unprocessed results
This chapter assumes you can already create an ingest job, upload CSV data, set UploadComplete, and poll the job until it reaches a terminal state. Here you will learn how to find out what happened to each row.
Three result sets
A finished ingest job keeps three CSV result sets. Each one has its own endpoint under the job.
| Result set | Endpoint | Contains |
|---|---|---|
| Successful | `/jobs/ingest/$JOB_ID/successfulResults/` | Rows that were saved |
| Failed | `/jobs/ingest/$JOB_ID/failedResults/` | Rows that Salesforce tried to save and rejected |
| Unprocessed | `/jobs/ingest/$JOB_ID/unprocessedrecords/` | Rows Salesforce never attempted |
Request results only when the job state is JobComplete, Failed or Aborted. Salesforce keeps the results for a limited time (seven days at the time of writing), so download them when the job finishes.
Extra columns
Each result file starts with columns that Salesforce adds, followed by the columns from your original CSV.
- Successful results add
sf__Id, the ID of the saved record, andsf__Created.sf__Createdistruewhen a record was created andfalsewhen an existing one was updated. It is most useful for upserts. - Failed results add
sf__Idandsf__Error.sf__Errorholds the reason for the failure, usually a status code followed by a message. - Unprocessed results have only your original columns, so you can resubmit the file as it is.
Here is a failed-results file from a Contact insert:
"sf__Id","sf__Error","FirstName","LastName","Email"
"","REQUIRED_FIELD_MISSING:Required fields are missing: [LastName]:LastName --","Grace","","[email protected]"Downloading the results
for kind in successfulResults failedResults unprocessedrecords; do
curl -s "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID/$kind/" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-o "$kind.csv"
doneNote that the unprocessed endpoint is spelled unprocessedrecords, in lowercase, with no Results suffix.
Reconciling the counts
The job status response reports numberRecordsProcessed and numberRecordsFailed. Compare these with the number of rows in each file. A quick check in Python:
import csv
def count_rows(path):
with open(path, newline="", encoding="utf-8") as f:
return sum(1 for _ in csv.DictReader(f))
submitted = count_rows("contacts.csv")
ok = count_rows("successfulResults.csv")
failed = count_rows("failedResults.csv")
unprocessed = count_rows("unprocessedrecords.csv")
print(submitted, ok + failed + unprocessed)Use csv.DictReader and not wc -l. A quoted field can contain a line break, so counting lines can give the wrong row count.
Common errors
- Requesting results too early. Poll until the job reaches a terminal state first.
- Treating
JobCompleteas "everything worked". A job can complete with many failed rows. Always checknumberRecordsFailed. - Ignoring unprocessed rows. A job that ends as
FailedorAbortedmay leave rows untouched. They do not appear in the failed file. - Parsing with a naive split on commas. Use a real CSV parser, because fields can contain commas and newlines.
Exercise
A job ends in the Aborted state. Which result file holds the rows that Salesforce never tried to save, and which extra columns does it have?
Show answer
The unprocessedrecords file. It has no extra columns, only your original CSV columns, so you can upload it as a new job after fixing the cause of the abort.