Sub topic 12
Composite Graph for larger transactional units
This chapter assumes you can build a standard composite request, chain subrequests with @{referenceId.field}, and read a compositeResponse. It also assumes you know how allOrNone works.
The problem Composite Graph solves
A standard /composite call is limited to 25 subrequests. With allOrNone set to true, all of them form one transaction. With it set to false, each subrequest can succeed or fail on its own. Neither setting fits a batch of several unrelated record groups. You may want each group to commit or roll back as a unit, without one bad group blocking the rest.
Composite Graph handles this. You send one request to /composite/graph that contains several independent graphs. Each graph is a set of dependent subrequests. The graph is the transaction boundary.
How a graph request is shaped
Each entry in graphs has a graphId and its own compositeRequest array. Inside a graph, the subrequests look like ordinary composite subrequests, with a method, url, referenceId, and body. Reference IDs work as before.
{
"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}" }
}
]
}
]
}Send it with the same headers you used for /composite:
curl -X POST "$INSTANCE_URL/services/data/v60.0/composite/graph" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d @graph.jsonThe graph above has only one entry, but you can add more entries to graphs.
Transaction and reference rules
- Each graph is all-or-none. If one subrequest in a graph fails, Salesforce rolls back that whole graph.
- A failed graph does not affect the others. They can still commit.
- References work only inside a graph. A subrequest cannot use
@{...}to read a result from a different graph. - Because graphs do not share data, they are independent. Do not rely on their order.
- The request as a whole can hold far more subrequests than a standard composite call. The documented ceiling at the time of writing is 500. Check the current limits for your API version before you design around that number.
Reading the response
The response has a graphs array. Each entry carries the graphId, an isSuccessful flag, and a graphResponse that holds the familiar compositeResponse. The outer HTTP status is usually 200 even when a graph fails, so check isSuccessful for every graph.
import os
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'graphs': [{
'graphId': 'graph1',
'compositeRequest': [{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Account',
'referenceId': 'acct',
'body': {'Name': 'Graph Account'},
}],
}]
}
resp = requests.post(base + '/services/data/v60.0/composite/graph',
headers=headers, json=payload, timeout=30)
resp.raise_for_status()
for graph in resp.json()['graphs']:
print(graph['graphId'], 'ok' if graph['isSuccessful'] else 'rolled back')
for sub in graph['graphResponse']['compositeResponse']:
print(' ', sub['referenceId'], sub['httpStatusCode'])In a failed graph, the subrequest that caused the failure shows its own error. The other subrequests in that graph typically come back as PROCESSING_HALTED, as they do under allOrNone.
Common mistakes
- Treating the HTTP 200 as success. Always check
isSuccessfulper graph. - Referencing
@{acct.id}from a different graph. The reference will not resolve. - Reusing a
referenceIdwithin the same graph. - Putting records that must succeed together in separate graphs. Group them in one graph.
Exercise
You need to create 40 Account and Contact pairs. Each pair must save together, but one invalid pair must not stop the others. Which resource fits, and how do you structure the request?
Show answer
Use Composite Graph. Create one graph per pair, each with an Account subrequest and a Contact subrequest that references the Account with @{...}. Each graph commits or rolls back on its own. Afterward, check isSuccessful on every graph and retry only the failed ones.