Sub topic 13
Choosing the right composite resource
This chapter assumes you can build a composite request, use @{referenceId.field} references, set allOrNone, and read a compositeResponse. It also assumes you know the earlier chapters' limits. Here you compare the five resources and pick one for a task.
The five resources side by side
All five send several operations in one HTTP call. They differ in three ways: whether subrequests can use each other's results, how failure is handled, and how much work one call can carry.
| Resource | Endpoint | Chaining with references | Failure behavior | Typical use |
|---|---|---|---|---|
| Composite | `/composite` | Yes | `allOrNone` true rolls back everything. False lets other subrequests continue. | Dependent steps such as create, then link, then read |
| Composite Batch | `/composite/batch` | No | Each subrequest is independent. `haltOnError` can stop the rest. | Unrelated reads and writes |
| sObject Tree | `/composite/tree/{sObject}` | Built into the nesting | All records save or none do | Creating a parent with its children |
| sObject Collections | `/composite/sobjects` | No | Controlled by `allOrNone` | Create, update, delete, or retrieve many records of the same kind |
| Composite Graph | `/composite/graph` | Yes, inside each graph | Each graph is its own transaction | Several dependent groups in one call |
Decision questions
Ask these in order.
- Does a later step need a value produced by an earlier one? If not, skip to question 3. If yes, you need references. Use Composite, or Composite Graph when you need more subrequests or several separate transactional groups.
- Is the dependency only parent-to-child creation? Then sObject Tree is simpler than writing the references yourself. Nest the children under the parent using the child relationship name, such as
Contacts. - Is it the same operation on many records of one object? Use sObject Collections. A single call handles up to 200 records.
- Are the operations unrelated? Use Composite Batch. A failure in one subrequest does not undo the others.
Worked comparison
Suppose you need to create an Account and a Contact that belongs to it. Three resources can do it.
- Composite: two subrequests with a reference. You also get a read-back subrequest if you want one.
- sObject Tree: one nested record. This is the shortest body, but it only creates records. You cannot add a read or update step.
- Composite Graph: the same two subrequests inside one graph. It makes sense only if you also need other independent graphs in the same call.
Now suppose you must load 150 Accounts from a spreadsheet and some rows may be invalid. sObject Collections with allOrNone set to false saves the valid rows. The response reports an error for each failed row. Composite would waste subrequests, because its limit is far below 150 records.
A mixed task, such as describing an object, creating one unrelated record, and running a query, fits Composite Batch. Nothing depends on anything else, and you do not want one failure to cancel the rest.
Common mistakes
- Treating the outer HTTP 200 as success. For Composite, check every subrequest's
httpStatusCode. A successful outer call can contain failed subrequests. - Expecting rollback from Batch or from Collections with
allOrNonefalse. Only transactional modes undo earlier work. - Using Tree for updates. Tree creates records only.
- Ignoring limits. Check the documented subrequest and record limits for the resource you chose before you design a payload. The limits differ between resources.
- Choosing Graph by default. It adds structure you may not need. Choose it when you have several independent dependent groups, not for one chain.
Summary rules
- Dependent steps with one transaction: Composite.
- Parent and children creation only: sObject Tree.
- Many records of one object: sObject Collections.
- Unrelated operations: Composite Batch.
- Multiple dependent groups, each with its own rollback: Composite Graph.
Exercise
You must create 30 unrelated Leads in one call. If some Leads fail validation, the rest should still save. You also want no subrequest limits tied to a chain of dependencies. Which resource do you pick, and which allOrNone value do you set?
Show answer
Use sObject Collections with allOrNone set to false. The 30 records are the same object and independent, and Collections handles up to 200 records per call. The false setting lets valid Leads save while the response reports an error for each invalid one. Composite Batch would also run them independently, but Collections is the purpose-built resource for many records of one object.