Sub topic 7
Handling partial failure and halted subrequests
This chapter assumes you can send a composite request and read the compositeResponse array, as covered in earlier chapters. Here you will learn how to find which subrequest failed and why the others did not run.
The outer call can succeed while work fails
A composite request returns HTTP 200 when Salesforce accepts and processes the request. The outcome of each subrequest is in its own httpStatusCode inside compositeResponse. Checking only the outer status will hide failures.
Each entry in compositeResponse has a referenceId, an httpStatusCode, httpHeaders, and a body. When a subrequest fails, the body is an array of error objects, each with an errorCode, a message, and often a fields list.
What allOrNone changes
The allOrNone flag decides what happens to the other subrequests after one fails.
| Setting | Failed subrequest | Other subrequests |
|---|---|---|
| `true` | Returns its real error | Everything is rolled back. Subrequests that would have succeeded return HTTP 400 with `PROCESSING_HALTED`. |
| `false` | Returns its real error | Independent subrequests still commit. Subrequests that use the failed one's result fail because the reference has no value. |
PROCESSING_HALTED is not the cause of the problem. It means "this subrequest was fine, but the transaction was rolled back because another one failed." The real cause is the one entry with a different error code.
Finding the root cause
With allOrNone: true, the failed subrequest is the one whose error code is not PROCESSING_HALTED. The following script classifies a response. It uses a sample response, so it runs without a Salesforce org.
def classify(composite_response):
ok, halted, failed = [], [], []
for sub in composite_response:
status = sub["httpStatusCode"]
if 200 <= status < 300:
ok.append(sub["referenceId"])
continue
errors = sub["body"] if isinstance(sub["body"], list) else []
codes = {e.get("errorCode") for e in errors}
if codes == {"PROCESSING_HALTED"}:
halted.append(sub["referenceId"])
else:
failed.append((sub["referenceId"], errors))
return ok, halted, failed
sample = [
{
"referenceId": "newAccount",
"httpStatusCode": 400,
"body": [{
"errorCode": "PROCESSING_HALTED",
"message": "The transaction was rolled back since another operation in the same transaction failed.",
}],
},
{
"referenceId": "newContact",
"httpStatusCode": 400,
"body": [{
"errorCode": "REQUIRED_FIELD_MISSING",
"message": "Required fields are missing: [LastName]",
"fields": ["LastName"],
}],
},
]
ok, halted, failed = classify(sample)
print("succeeded:", ok)
print("halted:", halted)
for ref, errors in failed:
print("root cause:", ref, errors[0]["errorCode"], errors[0]["message"])In this sample, newAccount looks like a failure but is only halted. The root cause is newContact, which has no LastName. If you retried newAccount alone, you would be fixing the wrong request.
Partial failure with allOrNone false
With allOrNone: false, some records can be saved while others are not. Before you retry, decide what is already committed:
- Collect the subrequests with a 2xx status. Those changes are saved and must not be created again.
- Collect the subrequests with a real error and fix their input.
- Collect the subrequests that failed because a reference had no value. They need the failed parent to succeed first.
- Send a new request containing only the failed work. Reuse IDs from the saved records instead of references to subrequests that no longer exist in the new call.
Common mistakes
- Treating the first non-2xx entry as the cause. With
allOrNone: true, halted entries can appear before the real failure in the array. - Assuming a 200 outer status means success.
- Logging only
httpStatusCode. Log theerrorCodeandmessagetoo, because they say whether a subrequest failed or was halted.
Exercise
A composite request with allOrNone true returns three subrequests. Two have status 400 with errorCode PROCESSINGHALTED. One has status 400 with errorCode DUPLICATEVALUE. Which subrequest do you fix, and has any data been saved?
Show answer
Fix the subrequest with DUPLICATEVALUE, because it is the root cause. The two PROCESSINGHALTED entries were valid but rolled back. No data has been saved, so you can resend the whole corrected request without creating duplicates.