Sub topic 12
Composite Graph for larger transactional units
This chapter covers the Composite Graph resource. It assumes you have read the earlier chapters on the structure of a composite request, @{referenceId.field} references, allOrNone, and the 25-subrequest limit. You should also have an OAuth access token and an instance URL for an org.
What problem Composite Graph solves
A regular /composite request is one transaction, and it is capped at 25 subrequests. If you need to create a larger set of related records, you hit that cap quickly. You also lose isolation. If you put two unrelated record sets into one request with allOrNone set to true, a failure in one set rolls back the other.
Composite Graph addresses both problems. You send several independent graphs in one HTTP call. Each graph holds its own list of subrequests, and each graph is its own transaction. The documented ceiling for a single graph is much higher than 25 subrequests. At the time of writing it is 500 nodes per graph. Check the REST API Developer Guide for your API version, because limits can change.
In short:
- A graph is a group of subrequests that can reference each other and succeed or roll back together.
- Graphs in the same request cannot reference each other and do not roll back each other.
- One HTTP call can carry several graphs, so you pay one round trip and one API call for the whole payload.
The endpoint and request shape
You send a POST to /services/data/v60.0/composite/graph. The body has a top-level graphs array. Each element has a graphId and a compositeRequest array. The compositeRequest array uses the same subrequest format you already know: method, url, referenceId, and body.
{
"graphs": [
{
"graphId": "graph1",
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Account",
"referenceId": "acct",
"body": { "Name": "Graph Account" }
},
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Contact",
"referenceId": "cont",
"body": { "LastName": "Okafor", "AccountId": "@{acct.id}" }
}
]
}
]
}Two details differ from /composite:
- There is no
allOrNoneproperty. Transactional behavior is built in, and it applies to each graph as a whole. graphIdmust be unique within the request, andreferenceIdvalues must be unique within their graph.
Send it the same way as any composite call:
curl -X POST "$INSTANCE_URL/services/data/v60.0/composite/graph" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d @graph.jsonHow graphs execute
Inside a graph, @{referenceId.field} works as it does in /composite. The server works out the order of execution from those references, so a subrequest runs after the ones it depends on. Across graphs there is no sharing. A reference such as @{acct.id} in graph2 cannot point to a subrequest in graph1, even when graph1 succeeds.
If a subrequest in a graph fails, Salesforce rolls back the whole graph. The failing subrequest reports its own error. The other subrequests in that graph report PROCESSING_HALTED, the same marker you see in a composite request that uses allOrNone: true. Other graphs in the request are not affected.
Two graphs in one request
The following request has one graph that creates an Account and Contact, and a second that creates an Account and an Opportunity. The graphs are unrelated, so a failure in one will not undo the other.
{
"graphs": [
{
"graphId": "customerA",
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Account",
"referenceId": "acct",
"body": { "Name": "Customer A" }
},
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Contact",
"referenceId": "cont",
"body": { "LastName": "Okafor", "AccountId": "@{acct.id}" }
}
]
},
{
"graphId": "customerB",
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Account",
"referenceId": "acct",
"body": { "Name": "Customer B" }
},
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Opportunity",
"referenceId": "opp",
"body": {
"Name": "Customer B renewal",
"StageName": "Prospecting",
"CloseDate": "2027-03-31",
"AccountId": "@{acct.id}"
}
}
]
}
]
}Both graphs reuse the reference IDs acct. That is allowed, because reference IDs only need to be unique within a graph.
Reading the graph response
The response does not use the compositeResponse shape at the top level. It wraps one result per graph in a graphs array. Each entry carries the graphId, an isSuccessful flag, and a graphResponse object that holds a compositeResponse array.
Here is an abbreviated response in which customerA succeeds and customerB fails because the Opportunity has an invalid stage name:
{
"graphs": [
{
"graphId": "customerA",
"graphResponse": {
"compositeResponse": [
{ "httpStatusCode": 201, "referenceId": "acct", "body": { "id": "001xx000003DGb1AAG", "success": true, "errors": [] } },
{ "httpStatusCode": 201, "referenceId": "cont", "body": { "id": "003xx000004TmiQAAS", "success": true, "errors": [] } }
]
},
"isSuccessful": true
},
{
"graphId": "customerB",
"graphResponse": {
"compositeResponse": [
{ "httpStatusCode": 400, "referenceId": "opp", "body": [ { "errorCode": "INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST", "message": "Stage: bad value for restricted picklist field", "fields": ["StageName"] } ] },
{ "httpStatusCode": 400, "referenceId": "acct", "body": [ { "errorCode": "PROCESSING_HALTED", "message": "The transaction was rolled back since another operation in the same transaction failed." } ] }
]
},
"isSuccessful": false
}
]
}The ordering of entries inside a failed graph can differ from the request order, so always match on referenceId and do not rely on array position. The exact error text in your org will vary. What you can rely on is the pattern: one real error, and PROCESSING_HALTED for the rest of that graph.
Checking results in code
The outer HTTP status can be 200 even when a graph failed. Check isSuccessful for each graph, and then look inside the graph for the real error.
import os
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'graphs': [
{
'graphId': 'customerA',
'compositeRequest': [
{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Account',
'referenceId': 'acct',
'body': {'Name': 'Customer A'},
},
{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Contact',
'referenceId': 'cont',
'body': {'LastName': 'Okafor', 'AccountId': '@{acct.id}'},
},
],
}
]
}
resp = requests.post(
base + '/services/data/v60.0/composite/graph',
headers=headers,
json=payload,
timeout=60,
)
resp.raise_for_status()
for graph in resp.json()['graphs']:
if graph['isSuccessful']:
print(graph['graphId'], 'committed')
continue
print(graph['graphId'], 'rolled back')
for sub in graph['graphResponse']['compositeResponse']:
body = sub['body']
if sub['httpStatusCode'] >= 400 and isinstance(body, list):
code = body[0].get('errorCode')
if code != 'PROCESSING_HALTED':
print(' cause:', sub['referenceId'], code, body[0].get('message'))The loop skips PROCESSING_HALTED entries so the printed output points at the subrequest that actually caused the rollback.
Constraints and gotchas
- Limits differ from
/composite. The 25-subrequest cap does not apply, but a graph has its own cap. Governor limits still apply to the work inside each transaction, for example DML row counts and CPU time in triggers and flows that your inserts fire. A graph with hundreds of record creations can hit those. - No cross-graph references. If two record sets depend on each other, they belong in the same graph. If they are independent, separate graphs give you isolation.
- A big graph is a big transaction. One failing record rolls back everything in the graph. If the records are only loosely related, split them into several graphs so one bad record does not undo the others.
- Retry per graph. Because each graph reports
isSuccessful, a client can resend only the failed graphs. Make sure retries are safe. Rolled-back graphs created nothing, but committed graphs did, so resending the whole request would duplicate records. - Not a bulk load tool. For tens of thousands of records, Bulk API 2.0 is the better fit. Composite Graph optimizes round trips and transactional grouping, not raw throughput.
When to pick Composite Graph
Choose Composite Graph when you need more than 25 dependent subrequests in a single transaction, or when you want several independent transactional groups in one call. For a handful of dependent steps, plain /composite with allOrNone: true is simpler. For one parent with children, sObject Tree is more compact. The final chapter compares all five resources side by side.
Exercise
A client sends a composite graph request with graphs orderA and orderB. A subrequest in orderB fails validation. The HTTP response is 200. What happened to the records in orderA, what does orderB report, and how should the client find the cause of the failure?
Show answer
orderA is unaffected. If none of its subrequests failed, it committed and reports isSuccessful: true. orderB rolled back as a whole and reports isSuccessful: false. Inside orderB's graphResponse.compositeResponse, the failing subrequest carries the real error code and message, and the other subrequests report PROCESSING_HALTED. The client should read the isSuccessful flag for each graph, then look for the entry that is not PROCESSING_HALTED, matching entries by referenceId. For a retry it should resend only orderB.
Exercise
You need to create 40 Contacts under one new Account, and all 41 records must be saved or none of them. Can you use /composite, and what would you use instead?
Show answer
No. /composite allows 25 subrequests per request, and this job needs 41. Use Composite Graph with a single graph that holds the Account subrequest and the 40 Contact subrequests, each Contact using @{acct.id} for AccountId. The graph is one transaction, so a failure rolls back all 41 records.