Sub topic 8
Subrequest limits and constraints
This chapter assumes you can build a composite request with reference IDs and read a compositeResponse. It covers the documented caps on request size, so you can design requests that Salesforce accepts.
Why limits matter
A composite call counts as one API call against your org's daily allocation. Each subrequest still does real work on the server, so Salesforce caps how much one call can carry. A request that exceeds a cap is rejected, or its extra subrequests fail. Check these numbers against the release documentation for your API version, because limits can change.
The main limits
| Resource | Documented cap |
|---|---|
| Composite | 25 subrequests per call |
| Composite, query and sObject Collections subrequests | At most 5 of the 25 |
| Composite Batch | 25 subrequests per call |
| sObject Collections | 200 records per call |
| sObject Tree | 200 records total across the tree |
| Composite Graph | 500 subrequests per call, spread across graphs |
Two details cause most surprises:
- The 5-subrequest sub-cap. A Composite request may contain 25 subrequests, but only 5 of them can be SOQL queries or sObject Collections operations. A request with 6 queries fails even though it has fewer than 25 subrequests.
- The 200-record caps. In sObject Tree, the 200 counts every record in the nested structure, parents and children together. One Account with 200 Contacts is 201 records and exceeds the cap.
Other constraints
- Every
referenceIdmust be unique within the request. - A reference such as
@{newAccount.id}can only point to a subrequest that appears earlier in the list. - In Composite Batch, subrequests cannot reference each other, so the limit of 25 applies to independent work.
- In Composite,
allOrNone: truecovers the whole request. If you split a request into chunks to stay under the limit, each chunk becomes its own transaction.
Checking limits before you send
Catching an oversized request on the client gives a clearer error than a rejection from the server. This function counts subrequests and checks the sub-cap.
MAX_SUBREQUESTS = 25
MAX_QUERY_LIKE = 5
def is_query_like(sub):
url = sub["url"]
return "/query" in url or "/composite/sobjects" in url
def check_composite(subrequests):
if len(subrequests) > MAX_SUBREQUESTS:
raise ValueError(f"{len(subrequests)} subrequests; the maximum is {MAX_SUBREQUESTS}")
query_like = sum(1 for s in subrequests if is_query_like(s))
if query_like > MAX_QUERY_LIKE:
raise ValueError(f"{query_like} query or collections subrequests; the maximum is {MAX_QUERY_LIKE}")
ids = [s["referenceId"] for s in subrequests]
if len(ids) != len(set(ids)):
raise ValueError("referenceId values must be unique")
subs = [
{"method": "GET", "url": f"/services/data/v60.0/query?q=SELECT+Id+FROM+Account+LIMIT+{n}",
"referenceId": f"q{n}"}
for n in range(1, 7)
]
try:
check_composite(subs)
except ValueError as err:
print("Rejected locally:", err)Running this prints Rejected locally: 6 query or collections subrequests; the maximum is 5.
Staying under the caps
Use these approaches when a request is too big:
- Replace many single-record subrequests with one sObject Collections call, which handles up to 200 records.
- Move independent reads into Composite Batch.
- Use Composite Graph for large dependent workloads, since it allows more subrequests per call.
- Chunk independent subrequests into several calls, and accept that each call is its own transaction.
Exercise
A Composite request has 12 subrequests: 7 SOQL queries and 5 record creations. Does it respect the limits? What would you change?
Show answer
It violates the sub-cap. The total of 12 is under 25, but only 5 subrequests may be queries or sObject Collections operations, and this request has 7 queries. Move 2 or more queries into a Composite Batch call (they are independent reads), or combine lookups into fewer queries. Then the request has at most 5 query-type subrequests.