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 recordA merge field, such as {{Opportunity.Account.Name}}
One list of child records, such as an opportunity's productsAdd Child on Gather Data and a TableStart/TableEnd row
Records you must filter in ways a relationship can'tA SOQL query with pv0
Records that each have their own lists of related recordsA nested query and a nested table (this guide)
Data computed or combined in codeAn 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}
  1. Type the query by hand in the solution's query window.
  2. 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.
  3. 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.
  4. Check the result with the SOQL query preview: enter a record ID in the pv0 field 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

  1. In Word, insert a one-column outer table with a single row. This row repeats once per child account.

  2. In that row's cell, type {{TableStart:ChildAccounts}}{{Name}} on the first line.

  3. Below it, in the same cell, insert an inner table for opportunities, with a header row and one data row:

    OpportunityStageClose date
    {{TableStart:ChildAccounts.Opportunities}}{{Name}}{{StageName}}{{CloseDate}}{{TableEnd:ChildAccounts.Opportunities}}
  4. Below it, in the same cell, insert an inner table for cases:

    CaseSubjectStatus
    {{TableStart:ChildAccounts.Cases}}{{CaseNumber}}{{Subject}}{{Status}}{{TableEnd:ChildAccounts.Cases}}
  5. Below it, in the same cell, insert an inner table for contacts:

    ContactEmail
    {{TableStart:ChildAccounts.Contacts}}{{Name}}{{Email}}{{TableEnd:ChildAccounts.Contacts}}
  6. 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

  1. On Assign Behaviors, in the query parameters, give pv0 its value, here {!Account.Id}. If you don't pass a value, Docs Made Easy uses the record ID by default, which also works here because ParentId should match the current account. If pv0 should be something other than the record ID, you must pass it, or the query can fail.
  2. 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

SymptomLikely causeFix
"Query could not be parsed"A typo, a stray :, wrong quotes, or a singular relationship name such as FROM Contact inside the subqueryDME-SF-001; test in the Developer Console
"No such column" or invalid fieldA field name is wrong, or you can't read itDME-SF-002
An object or relationship isn't recognizedWrong relationship name, or no access to the objectDME-SF-003
The outer table works but an inner table is emptyInner tags in the wrong row or with the wrong name, or a filter or limit leaves no rowsRelated-list tables are empty, show one row or repeat
Rows from other accounts appearThe WHERE ParentId = {pv0} filter is missing from the queryFilter a query to the current record (pv0)
"Tables inside other tables are not supported"The template is PowerPoint or ExcelUse 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 LIMIT and 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.

Next steps