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.

Chain Operations with Multiple Field Reference Patterns 

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.

Note

Example: Get Accounts Owned by a User 

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}.

Get Accounts Owned by Current User
1query queries {
2  uiapi {
3    currentUser {
4      Id
5      Name { value }
6      Username { value }
7    }
8    query {
9      allAccounts: Account(
10        where: { OwnerId: { eq: "@{currentUser.Id}" } }
11      ) {
12        edges {
13          node {
14            Id
15            Name { value }
16            Description { value }
17            Owner {
18              Id
19              Name { value }
20            }
21          }
22        }
23      }
24    }
25  }
26}

The response looks like this.

Chained Current User and Accounts Response
1{
2  "data": {
3    "uiapi": {
4      "currentUser": {
5        "Id": "005LT00000472u1YAA",
6        "Name": {
7          "value": "Your Name Here"
8        },
9        "Username": {
10          "value": "yournamehere@yourorg.com"
11        }
12      },
13      "query": {
14        "allAccounts": {
15          "edges": [
16            {
17              "node": {
18                "Id": "001LT00000DzUzNYAV",
19                "Name": {
20                  "value": "Global Media"
21                },
22                "Description": {
23                  "value": "GBM is the worldwide leader in technology news and information."
24                },
25                "Owner": {
26                  "Id": "005LT00000472u1YAA",
27                  "Name": {
28                    "value": "Your Name Here"
29                  }
30                }
31              }
32            },
33            {
34              "node": {
35                "Id": "001LT00000DzUzPYAV",
36                "Name": {
37                  "value": "Second Account"
38                },
39                "Description": {
40                  "value": "Second account description."
41                },
42                "Owner": {
43                  "Id": "005LT00000472u1YAA",
44                  "Name": {
45                    "value": "Your Name Here"
46                  }
47                }
48              }
49            },
50            {
51              "node": {
52                "Id": "001LT00000DzUzSYAV",
53                "Name": {
54                  "value": "Third Account"
55                },
56                "Description": {
57                  "value": null
58                },
59                "Owner": {
60                  "Id": "005LT00000472u1YAA",
61                  "Name": {
62                    "value": "Your Name Here"
63                  }
64                }
65              }
66            }
67          ]
68        }
69      }
70    }
71  },
72  "errors": [],
73  "extensions": {}
74}

Example: Get Accounts Owned by a List of Users 

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.

Get Accounts Owned by Users
1query queries {
2  uiapi {
3    query {
4      User(
5        where: { LastName: { like: "User%" } }
6      ) {
7        edges {
8          node {
9            Id
10            FirstName { value }
11            LastName { value }
12            Email { value }
13          }
14        }
15      }
16      Account(
17        where: { OwnerId: { in: ["@{User.edges[*].node.Id}"] } }
18      ) {
19        edges {
20          node {
21            Id
22            Name { value }
23            Description { value }
24            Owner {
25              Id
26              Name { value }
27            }
28          }
29        }
30      }
31    }
32  }
33}

The response looks like this.

Chained Users and Accounts Response
1{
2  "data": {
3    "uiapi": {
4      "query": {
5        "User": {
6          "edges": [
7            {
8              "node": {
9                "Id": "005LT00000472vhYAA",
10                "FirstName": {
11                  "value": "Your Name"
12                },
13                "LastName": {
14                  "value": "User"
15                },
16                "Email": {
17                  "value": "yournameuser@yourorg.com"
18                }
19              }
20            },
21            {
22              "node": {
23                "Id": "005LT00000472viYAA",
24                "FirstName": {
25                  "value": "Another Name"
26                },
27                "LastName": {
28                  "value": "User"
29                },
30                "Email": {
31                  "value": "anothernameuser@yourorg.com"
32                }
33              }
34            },
35            {
36              "node": {
37                "Id": "005LT00000472vjYAA",
38                "FirstName": {
39                  "value": "Third Name"
40                },
41                "LastName": {
42                  "value": "User"
43                },
44                "Email": {
45                  "value": "thirdnameuser@yourorg.com"
46                }
47              }
48            }
49          ]
50        },
51        "Account": {
52          "edges": [
53            {
54              "node": {
55                "Id": "001LT000011naPRYAY",
56                "Name": {
57                  "value": "Account1"
58                },
59                "Description": {
60                  "value": null
61                },
62                "Owner": {
63                  "Id": "005LT00000472vhYAA",
64                  "Name": {
65                    "value": "Your Name User"
66                  }
67                }
68              }
69            },
70            {
71              "node": {
72                "Id": "001LT00000DzUzOYAV",
73                "Name": {
74                  "value": "Account2"
75                },
76                "Description": {
77                  "value": "Account2 description."
78                },
79                "Owner": {
80                  "Id": "005LT00000472viYAA",
81                  "Name": {
82                    "value": "Another Name User"
83                  }
84                }
85              }
86            },
87            {
88              "node": {
89                "Id": "001LT00000DzUzQYAV",
90                "Name": {
91                  "value": "Account3"
92                },
93                "Description": {
94                  "value": "Account3 description."
95                },
96                "Owner": {
97                  "Id": "005LT00000472vjYAA",
98                  "Name": {
99                    "value": "Third Name User"
100                  }
101                }
102              }
103            }
104          ]
105        }
106      }
107    }
108  },
109  "errors": [],
110  "extensions": {}
111}