Sub topic 5
Reading the composite response
This chapter assumes you can send a composite request and know the structure from the earlier chapters: allOrNone, compositeRequest, and referenceId. Here you learn how to interpret what comes back.
The outer status is not enough
A call to /services/data/v60.0/composite returns HTTP 200 whenever Salesforce can parse the request and process it. That includes cases where individual subrequests fail. If you check only the outer status, you will miss errors.
The body contains one array, compositeResponse. It has one entry for each subrequest, in the same order as the request.
{
"compositeResponse": [
{
"body": { "id": "001xx000003DGb2AAG", "success": true, "errors": [] },
"httpHeaders": { "Location": "/services/data/v60.0/sobjects/Account/001xx000003DGb2AAG" },
"httpStatusCode": 201,
"referenceId": "newAccount"
},
{
"body": [
{
"errorCode": "REQUIRED_FIELD_MISSING",
"message": "Required fields are missing: [LastName]",
"fields": ["LastName"]
}
],
"httpHeaders": {},
"httpStatusCode": 400,
"referenceId": "newContact"
}
]
}Fields of each entry
| Field | Meaning |
|---|---|
| `referenceId` | The ID you gave the subrequest. Use it to match entries, not the position alone. |
| `httpStatusCode` | The status that the same call would return on its own, such as 200, 201, 204, or 400. |
| `body` | The payload. It is an object on success and usually an array of error objects on failure. |
| `httpHeaders` | Headers for the subrequest. A successful create includes `Location`. |
Notice that body changes shape. A successful POST returns an object with id, success, and errors. A failed one returns a list of objects with errorCode, message, and often fields. A 204 response has a null body. Your parser must handle all three.
A decision path
Finding the root cause
With allOrNone set to true, one failure rolls back the whole request. Every other subrequest then reports status 400 with the error code PROCESSING_HALTED. The message says the transaction was rolled back because another operation failed. These entries are not real faults. The one entry with a different error code is the cause. Report that one first.
With allOrNone set to false, successful subrequests stay saved. Subrequests that use @{referenceId.field} from a failed one also fail, because the referenced value does not exist.
A reusable parser
This script prints every subrequest, separates root causes from halted ones, and exits with an error if anything failed.
import os
import sys
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'allOrNone': True,
'compositeRequest': [
{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Account',
'referenceId': 'newAccount',
'body': {'Name': 'Acme Corp'},
},
{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Contact',
'referenceId': 'newContact',
'body': {'AccountId': '@{newAccount.id}'},
},
],
}
resp = requests.post(base + '/services/data/v60.0/composite',
headers=headers, json=payload, timeout=30)
resp.raise_for_status()
failed = False
for sub in resp.json()['compositeResponse']:
ref, code, body = sub['referenceId'], sub['httpStatusCode'], sub['body']
if 200 <= code < 300:
print(ref, code, 'ok')
continue
failed = True
errors = body if isinstance(body, list) else [body]
for err in errors:
kind = 'halted' if err.get('errorCode') == 'PROCESSING_HALTED' else 'ROOT CAUSE'
print(ref, code, kind, err.get('errorCode'), err.get('message'))
sys.exit(1 if failed else 0)The Contact here omits LastName on purpose. The Account is rolled back with PROCESSING_HALTED, and the Contact is reported as the root cause.
Common mistakes
- Treating
resp.okas success for every subrequest. - Reading
body['id']without checking the status code first, which raises an error on failures. - Logging only the first error and hiding the real cause behind a
PROCESSING_HALTEDentry.
Exercise
A composite request with allOrNone true has three subrequests. The outer call returns 200. The compositeResponse shows 400 PROCESSINGHALTED for the first and third entries, and 400 DUPLICATEVALUE for the second. Which subrequest caused the failure, and is the first one's data saved?
Show answer
The second subrequest caused it, because DUPLICATEVALUE is a real error and PROCESSINGHALTED only marks rollbacks. The first subrequest's data is not saved, since allOrNone true rolled back the whole transaction.