For template authors comfortable with SOQL: show several levels of related data in one document, for example an account summary that lists each child account followed by its open opportunities, recent cases and contacts. You write one nested query (a subquery) and lay it out as a table inside a table. Allow about 30 minutes for the first one.
Nested queries arrived in v1.19.0 and nested table syntax in v1.23.1.
Before you begin
- Docs Made Easy 1.23.1 or later, so you have both features.
- A working document query. If you haven't created one, start with Pull data into a template with a SOQL query and Filter a query to the current record (pv0).
- A Word (.docx) template. Nested tables work in Word only, not in Excel or PowerPoint templates (DME-TPL-000). Nested queries and nested tables work in both Basic and Pro.
- A test record with data at every level, for example a parent account with child accounts that have opportunities, cases and contacts.
When to use a nested query
| If you need… | Use |
|---|---|
| Fields on the record or a parent record | A merge field, such as {{Opportunity.Account.Name}} |
| One list of child records, such as an opportunity's products | Add Child on Gather Data and a TableStart/TableEnd row |
| Records you must filter in ways a relationship can't | A SOQL query with pv0 |
| Records that each have their own lists of related records | A nested query and a nested table (this guide) |
| Data computed or combined in code | An Apex class |
For the full comparison, see Choose a data source.
Write the nested query
In SOQL, a subquery is a SELECT in parentheses inside the main query. It reads from a child relationship name, which is plural for standard objects (Opportunities, Cases, Contacts) and ends in __r for custom ones. This is standard Salesforce SOQL.
You type a nested query by hand, including any filters. This one returns each child account of the current account, with its open opportunities, its five most recent cases and its contacts:
SELECT Id, Name,
(SELECT Name, StageName, CloseDate FROM Opportunities WHERE IsClosed = false),
(SELECT CaseNumber, Subject, Status FROM Cases ORDER BY CreatedDate DESC LIMIT 5),
(SELECT Name, Email FROM Contacts)
FROM Account
WHERE ParentId = {pv0}
- Type the query by hand in the solution's query window.
- Test it in the Salesforce Developer Console (Query Editor) with a real account ID in single quotes in place of
{pv0}. If Salesforce rejects it there, it fails in Docs Made Easy too. - Save it with a short, unique alias, for example
ChildAccounts. The alias is required: the template's table tags use it. Saving creates a Document Query record. - Check the result with the SOQL query preview: enter a record ID in the
pv0field and click Preview.
You can nest as many levels as SOQL allows. Docs Made Easy doesn't add its own limit.
Build the nested table in Word
The rule:
- The outer table uses the query's alias:
{{TableStart:ChildAccounts}}…{{TableEnd:ChildAccounts}}. - Each inner table uses the alias, a dot, and the child relationship name from the subquery:
{{TableStart:ChildAccounts.Opportunities}}…{{TableEnd:ChildAccounts.Opportunities}}.
The Merge Field Helper also gives you the nested syntax: in the template step, select your query on the SOQL Query Fields tab and copy the table syntax. The v1.23.1 release notes show an example.
Example: child accounts with their opportunities, cases and contacts
In Word, insert a one-column outer table with a single row. This row repeats once per child account.
In that row's cell, type
{{TableStart:ChildAccounts}}{{Name}}on the first line.Below it, in the same cell, insert an inner table for opportunities, with a header row and one data row:
Opportunity Stage Close date {{TableStart:ChildAccounts.Opportunities}}{{Name}}{{StageName}}{{CloseDate}}{{TableEnd:ChildAccounts.Opportunities}}Below it, in the same cell, insert an inner table for cases:
Case Subject Status {{TableStart:ChildAccounts.Cases}}{{CaseNumber}}{{Subject}}{{Status}}{{TableEnd:ChildAccounts.Cases}}Below it, in the same cell, insert an inner table for contacts:
Contact Email {{TableStart:ChildAccounts.Contacts}}{{Name}}{{Email}}{{TableEnd:ChildAccounts.Contacts}}At the very end of the outer cell, after the last inner table, type
{{TableEnd:ChildAccounts}}.
Keep each inner table's start and end tags in the same inner row, and the outer table's start and end tags in the same outer row. A tag in the wrong row is the most common reason a table comes out empty or repeated.
For an inner list that's often empty, decide what happens to the table: use Keep Table or Delete Table in the query builder, or TableHide.
Add pv0 and test
- On Assign Behaviors, in the query parameters, give
pv0its value, here{!Account.Id}. If you don't pass a value, Docs Made Easy uses the record ID by default, which also works here becauseParentIdshould match the current account. Ifpv0should be something other than the record ID, you must pass it, or the query can fail. - Upload the template, open the parent account, and click the button. From version 1.25, use the merge preview to check the result before you generate.
Verify it worked
The document lists each child account once, and under each one only that account's open opportunities, up to five cases and its contacts. No rows from other accounts appear, and an account without cases shows an empty or hidden cases table, depending on your Keep Table or Delete Table choice.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| "Query could not be parsed" | A typo, a stray :, wrong quotes, or a singular relationship name such as FROM Contact inside the subquery | DME-SF-001; test in the Developer Console |
| "No such column" or invalid field | A field name is wrong, or you can't read it | DME-SF-002 |
| An object or relationship isn't recognized | Wrong relationship name, or no access to the object | DME-SF-003 |
| The outer table works but an inner table is empty | Inner tags in the wrong row or with the wrong name, or a filter or limit leaves no rows | Related-list tables are empty, show one row or repeat |
| Rows from other accounts appear | The WHERE ParentId = {pv0} filter is missing from the query | Filter a query to the current record (pv0) |
| "Tables inside other tables are not supported" | The template is PowerPoint or Excel | Use separate tables or a Word template (DME-TPL-000) |
Limits and gotchas
- Salesforce SOQL limits apply. Each subquery adds records to the query result. Keep
LIMITand filters on the subqueries, and add only the fields you show. - Scope the main query. Without
WHERE ParentId = {pv0}or a similar filter, the query returns accounts from across the org. - Keep tables to a reasonable size. Up to about 500 rows per table works well; beyond that, performance may suffer.
- Word only. Nested tables don't work in Excel templates. Excel uses a different syntax for query data:
&=${{alias.Field}}. See SOQL query merge fields in Excel.