Chain Queries by Using Field References
| AVAILABLE API VERSION |
|---|
| API v67.0 and later |
Create a query that uses field references from a previous query. You can chain multiple operations that depend on a previous query’s results in the same transaction. You can reference any field, including fields in result lists, from a previous query. Chained queries can reference any supported objects, including objects that don’t have parent and child relationships.
There are multiple ways to reference fields, including fields in lists, in chained queries. Assuming ref is the name or alias of a previous query from the same request, supported field reference patterns are:
@{ref.FieldName}- First-level field reference@{ref.FieldName.value}- First-level field value reference@{ref.edges[*].node.FieldName}- Field reference in a list@{ref.edges[*].node.FieldName.value}- Field value reference in a list@{ref.edges[ListItemNumber].node.FieldName}- Field reference with a specific list item number, starting with 0@{ref.edges[ListItemNumber].node.FieldName.value}- Field value reference with a specific list item number, starting with 0
When chaining operations, place an operation after the operation it references. If you order an operation that references another operation first, the request fails because the operation can’t find the referenced operation.
This example chains the query of the current user to the query of all the accounts the user owns. The first query gets the current user. The second query gets the accounts owned by the current user by referencing the current user’s ID with @{currentUser.Id}.
The response looks like this.
This example chains the query of a list of users to the query of the accounts that the users own. The first query gets a subset of users in a list. The second query gets the accounts by referencing the owners’ IDs with @{User.edges[*].node.Id}.
It’s possible to rewrite this example as a single query. For illustrative purposes, the example shows how to reference a field in a result list of a previous query.
The response looks like this.