Sub topic 6
Rollback control with allOrNone
This chapter assumes you can send a composite request and know how referenceId values link subrequests. It covers the allOrNone flag, which decides whether a failure undoes the work that came before it.
What the flag does
allOrNone is a boolean at the top level of the composite request body. It defaults to false.
true: the subrequests run as one transaction. If any subrequest fails, Salesforce rolls back every change the request made.false: each subrequest is committed on its own. A failure does not undo the subrequests that succeeded.
A request that fails
This request creates an Account, then a Contact without the required LastName. The Contact will fail.
{
"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": { "FirstName": "Sam", "AccountId": "@{newAccount.id}" }
}
]
}With allOrNone set to true, the response looks like this (shortened):
{
"compositeResponse": [
{
"body": [
{
"errorCode": "PROCESSING_HALTED",
"message": "The transaction was rolled back since another operation in the same transaction failed."
}
],
"httpHeaders": {},
"httpStatusCode": 400,
"referenceId": "newAccount"
},
{
"body": [
{
"errorCode": "REQUIRED_FIELD_MISSING",
"message": "Required fields are missing: [LastName]",
"fields": ["LastName"]
}
],
"httpHeaders": {},
"httpStatusCode": 400,
"referenceId": "newContact"
}
]
}The Account subrequest did run, but its result was rolled back. Its entry reports PROCESSING_HALTED with status 400 instead of the original success. No Account exists in the org afterward. To find the real cause, look for the entry whose error code is not PROCESSING_HALTED.
The same request with allOrNone false
If you change the flag to false, the Account is created and returns 201. The Contact still fails with REQUIRED_FIELD_MISSING. The Account stays in the org.
A failure can also spread through references. If a subrequest uses @{newAccount.id} and newAccount failed, there is no ID to substitute, so the dependent subrequest fails too. Subrequests that do not depend on the failed one still run.
Comparing the two modes
| Aspect | `allOrNone: true` | `allOrNone: false` |
|---|---|---|
| Commit behavior | One transaction | Per subrequest |
| After a failure | Everything rolls back | Successes stay committed |
| Other subrequests' status | 400 `PROCESSING_HALTED` | Their own result |
| Typical use | Related records that must exist together | Independent operations |
Choosing a value
Use true when partial results would leave bad data. Creating an Account and its Contact is a typical case, because an orphaned Account may break later logic. Use false when subrequests are unrelated and you want as much work done as possible, such as updating several unrelated records.
Reading results in code
This Python check works for both modes. It finds the failures and separates the root cause from halted subrequests.
import os
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/Contact',
'referenceId': 'badContact',
'body': {'FirstName': 'Sam'},
}
],
}
resp = requests.post(base + '/services/data/v60.0/composite',
headers=headers, json=payload, timeout=30)
resp.raise_for_status()
for sub in resp.json()['compositeResponse']:
if sub['httpStatusCode'] >= 400:
code = sub['body'][0]['errorCode']
label = 'halted' if code == 'PROCESSING_HALTED' else 'root cause'
print(sub['referenceId'], code, label)Exercise
A composite request has allOrNone set to true and three subrequests. The second one fails validation. What does the response contain for the first and third subrequests, and is the first record saved?
Show answer
The first and third entries return HTTP status 400 with the error code PROCESSING_HALTED. Only the second entry carries the real validation error. The first record is not saved, because the whole transaction is rolled back. The outer HTTP response is still 200.