Sub topic 6
Rollback control with allOrNone
This chapter covers the allOrNone flag on the Composite resource. You'll learn what it does to the database, how each subrequest's result changes, and how to read the response. I assume you can already build a composite request and chain subrequests with @{referenceId.field}. I also assume you know that the outer HTTP call can return 200 while individual subrequests fail.
What the flag controls
allOrNone is an optional Boolean at the top level of a composite request body. It decides what happens when one subrequest fails.
false(the default): each subrequest is processed on its own. A failure affects only that subrequest and any later subrequest that depends on it. Everything else is saved.true: the request behaves as one transaction. If any subrequest fails, Salesforce rolls back every change made earlier in the request and does not run the remaining subrequests.
The flag sits next to compositeRequest, not inside a subrequest. You can't mix modes in one call.
{
"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": { "LastName": "Rivera", "AccountId": "@{newAccount.id}" }
},
{
"method": "GET",
"url": "/services/data/v60.0/sobjects/Contact/@{newContact.id}?fields=Id,LastName,AccountId",
"referenceId": "readContact"
}
]
}With allOrNone set to true, you never end up with an Account that has no Contact. Either all three subrequests succeed, or no record is saved.
When to choose each mode
The right setting depends on whether the subrequests form one business unit.
| Situation | Setting | Reason |
|---|---|---|
| Creating a parent and children that make no sense alone | `true` | An orphaned parent would be bad data. |
| Updating several unrelated records | `false` | One bad record shouldn't block the others. |
| Read-only subrequests (GET) | Either | There is nothing to roll back. |
| Chain where later steps depend on earlier ones | Usually `true` | A failed step would make the dependents fail anyway. |
Using true for everything is tempting, but it makes one invalid row cancel a whole request. Using false for everything can leave half-finished data that you must clean up later. Decide by asking what state you can accept if the call stops halfway.
What a failure looks like with allOrNone true
Suppose the Contact subrequest omits LastName, which is required. The request runs in order:
- The Account is created inside the transaction.
- The Contact fails with a required-field error.
- Salesforce rolls back the Account.
- The final GET is not executed.
The outer call still returns HTTP 200. The detail is in compositeResponse:
{
"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"
},
{
"body": [
{
"errorCode": "PROCESSING_HALTED",
"message": "The transaction was rolled back since another operation in the same transaction failed."
}
],
"httpHeaders": {},
"httpStatusCode": 400,
"referenceId": "readContact"
}
]
}Three points matter here:
- The subrequest that actually failed carries the real error code. Every other subrequest reports
PROCESSING_HALTED. - The Account subrequest reports 400 even though it ran without error. Its change was undone, so the response doesn't claim it succeeded.
- To find the cause, look for the entry whose
errorCodeis notPROCESSING_HALTED.
What a failure looks like with allOrNone false
With false, subrequest 1 keeps its saved Account. The failing Contact returns its own error. A later subrequest that uses @{newContact.id} can't resolve the reference, so it returns an error too, with a 4xx status. Check the exact errorCode in your response rather than hard-coding it. A later subrequest that doesn't use the failed result runs normally and can succeed.
This is the mode where you must inspect every entry. A response with a mix of 201, 400, and 200 statuses is normal, and the Account from step 1 is now committed.
Reading the outcome in code
Because the outer status is 200 in both modes, raise_for_status() is not enough. This script posts a request, then decides whether the whole transaction succeeded.
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/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()
results = resp.json()['compositeResponse']
failed = [r for r in results if r['httpStatusCode'] >= 400]
if not failed:
print('All subrequests succeeded')
else:
root_causes = [
r for r in failed
if not any(e.get('errorCode') == 'PROCESSING_HALTED' for e in r['body'])
]
for r in root_causes:
print('Root cause:', r['referenceId'], r['body'])
halted = [r['referenceId'] for r in failed if r not in root_causes]
print('Rolled back or halted:', halted)The Contact here has no LastName, so the script prints the Contact as the root cause and the Account as halted. If you change allOrNone to False, the Account is saved and the script lists only the failed entries. The root_causes filter would then separate real errors from nothing, because no PROCESSING_HALTED entries appear. Record which IDs were created, and decide whether to retry or clean up.
Edge cases and gotchas
- Server-side logic joins the transaction. Triggers, flows, validation rules, and duplicate rules run as part of each save. If one of them throws an error on a later subrequest, the earlier work is rolled back under
true. - Rollback is not retry. After a rolled-back request, the IDs from earlier subrequests no longer exist. Don't store them. Resending the same body is safe, but fix the failing record first.
- Unique reference IDs still matter. A duplicate
referenceIdis a request error. Fix the body before thinking about rollback. - Limits apply either way. The subrequest cap and other constraints (next chapters) are checked regardless of
allOrNone. A request over the limit is rejected before any subrequest runs. - Side effects outside the database may not undo. Rollback reverses database changes in the transaction. Don't assume it reverses actions such as callouts or emails made by your own automation. Check how those are built.
- Other resources use their own flags. sObject Collections accepts
allOrNonein its body, and its behavior is per record set. Composite Batch hashaltOnError, which stops later subrequests but does not roll back earlier ones, because batch subrequests are independent. Don't carry the Composite meaning ofallOrNoneover to those resources without reading their documentation.
Choosing between modes in practice
A short checklist helps:
- List the records the request would create or change.
- Ask what state is acceptable if only the first half succeeded.
- If any partial state is harmful, use
true. - If subrequests are independent, use
falseand handle each result. Consider whether Composite Batch or Collections fits better. - Write your client code to inspect every entry, so you can switch modes without surprises.
Exercise
A composite request has allOrNone set to true and three subrequests: create Account, create Contact (with an invalid email format), and read the Contact. The call returns HTTP 200. What is the state of the org, and which entry in compositeResponse tells you why?
Show answer
No Account or Contact exists, because the failure rolled back the whole transaction. The Contact entry carries the real error code (for example an invalid email error). The Account and read entries show PROCESSINGHALTED with status 400, so you identify the cause by finding the entry that is not PROCESSINGHALTED.
Exercise
You change the same request to allOrNone false. The Account is created, the Contact fails, and the read subrequest uses @{newContact.id}. What do you expect in the response and in the org?
Show answer
The Account entry shows a success status (201) and the Account remains saved. The Contact entry shows its own 400 error. The read subrequest fails because it can't resolve the reference to the Contact's ID. The org keeps an Account with no Contact, so your client must decide whether to retry the Contact or delete the Account.
Exercise
Why is checking resp.raiseforstatus() alone not enough after posting to /composite?
Show answer
The outer HTTP status is 200 even when subrequests fail or are rolled back. Only the httpStatusCode of each entry in compositeResponse shows whether each subrequest succeeded.