Newer Version Available
Handle Failed Records in Batches
A batch can have a Completed state even if some or all of the records failed. If a subset of records
failed, the successful records aren't rolled back. Likewise, even if the batch has a Failed state or if a job is aborted, some records could have completed
successfully.
When you get the batch results, it's important to look at the Success field for each result row to ensure that all rows were processed successfully. If a record wasn’t processed successfully, the Error column includes more information about the failure.
To identify failed records and log them to an error file:
- Wait for the batch to finish processing. See Get Information for a Batch and Interpret Batch State.
-
Get the batch results.
This sample CSV batch result shows an error for the last record because the LastName field was missing:
1"Id","Success","Created","Error" 2"003D000000Q89kQIAR","true","true","" 3"003D000000Q89kRIAR","true","true","" 4"","false","false","REQUIRED_FIELD_MISSING:Required fields are missing: [LastName]:LastName --" - Parse the results for each record:
- Track the record number for each result record. Each result record corresponds to a record in the batch. The results are returned in the same order as the records in the batch request. It's important to track the record number in the results so that you can identify the associated failed record in the batch request.
- If the Success field is false, the row wasn’t processed successfully. Otherwise, the record was processed successfully, and you can proceed to check the result for the next record.
- Get the contents of the Error column.
- Write the contents of the corresponding record in the batch request to an error file on your computer. Append the information from the Error column. If you don't cache the batch request that you submitted, you can retrieve the batch request from Salesforce.
After you examine each result record, you can manually fix each record in the error file and submit these records in a new batch. To check that each record processed successfully, repeat the previous steps.