Every document starts from the record you click the button on. When you need data beyond that record's fields, you choose a data source; pick the simplest that works, because relationships need no code or query and cover most templates.

The data sources

Fields on the record and its parents

Fields on the record need no setup: {{Opportunity.Name}}. For a parent record, add the relationship: {{Opportunity.Account.Name}}, or {{Invoice__r.Total_Amount__c}} for a custom lookup. Pick these in the Merge Field Helper, or use Add Lookup on Gather Data. See Template syntax basics.

Child relationships

On Gather Data, click Add Child to add a related list, such as Opportunity Products. You can set an alias, a filter, a sort order and what happens when there are no records. The records appear as a repeating table row.

You don't always need Add Child. The related-list form, such as {{TableStart:Account.r.Contacts}}, fetches all related records. Use the alias form (a child relationship or a query) when you want to sort, filter or limit the rows. The Merge Field Helper supports both forms. See Add fields from related records.

SOQL queries (document queries)

A query fetches records with your own fields, filters, sort order and limit, including records that aren't children of the current record. See Pull data into a template with a SOQL query.

pv0: filter a query to the current record

pv0 points a query at the record you generate from, for example SELECT Id, Name FROM Account WHERE Id = {pv0}. Without a pv0 filter, a query returns every matching record in the org, not just this account's contacts. Give pv0 its value, such as {!Account.Id}, in the solution's Assign Behaviors query parameters. If you don't pass a value, the record ID is used, which works when the filter is the record ID. When pv0 should be something else, pass it, or the query can fail. Use pv1, pv2 and so on for more filters. See Filter a query to the current record (pv0).

QVars: a query result for another parameter

A QVar doesn't put data in the template. It runs a query at generation time and hands the result to another parameter: QVar0Id holds the query ID and {QVar0} is the result, for example EmailToId = {QVar0} to choose the recipient or documenttemplateid = {qvar0} to choose templates. See Use QVars and Choose templates dynamically with a QVar query.

Apex classes

When data must be computed or combined in code, write a class that implements hic_docmerge.IDmeApexDataSourceProvider and returns a Map<String, Object> of data sets, then register it on Gather Data with New Apex Data. To filter the Apex data to the current record, set :recordid to the record's ID, for example {!Account.Id} to fetch the contacts of the account you generate from. This :recordid setting is unrelated to DME-SF-001, which is about a colon inside a SOQL query. Salesforce doesn't allow writing Apex directly in production: develop it in a sandbox and deploy it with tests covering at least 75% of your Apex code. See Merge data returned by an Apex class.

Salesforce reports

Add the report's ID to the button URL as the reportid parameter. Tabular and summary reports are supported. See How do I add Salesforce report data to a document?.

Static resources

For content that's the same in every document, such as a logo or legal text, upload a static resource once and reference it with {{StaticResource.Name}}, {{Image:StaticResource.Name}} or {{HTML:StaticResource.Name}}. See Create a static resource.

When to use which

If you need…Use
A field on the record or a parent recordA merge field, with Add Lookup if needed
All child records, with a simple filter or sortAdd Child on Gather Data
Records filtered in ways a relationship can't, or not directly relatedA SOQL query with pv0
A value for another parameter (recipient, template ID)A QVar
Data computed or combined in codeAn Apex class
Data from an existing tabular or summary reportreportid
The same logo or text in every documentA static resource

Three ways to create a query, and the alias

The guides create queries in three places: Create New Query in the wizard (pv0 guide), Gather Data > SOQL Queries (SOQL guide), and the Document Queries tab > New with SOQL typed by hand (QVar guide). A SOQL query creates a Document Query record. Use the solution's query builder for template data; the QVar guide uses the Document Queries tab because it needs the Document Query ID.

A SOQL query needs an alias, because the merge syntax uses it to fetch the query's data, for example &=${{QueryAlias.Field}}. Give each query and child object a short, unique alias.

Example: an account's contacts in an Excel template

  1. Create a query on Contact with the alias query, filtered to the current account with pv0:

    SELECT Id, LastName, FirstName, Name FROM Contact WHERE Account.Id = '{pv0}'
    
  2. In the Excel template, add one row:

    Last NameFirst NameFull Name
    &=${{query.LastName}}&=${{query.FirstName}}&=${{query.Name}}
  3. In the solution's Assign Behaviors query parameters, give pv0 the value {!Account.Id}. Upload the template and generate from Burlington Textiles Corp of America.

The output has one row per contact of that account only, for example:

Last NameFirst NameFull Name
RogersJackJack Rogers

For a Word template, the SOQL Query Fields tab of the Merge Field Helper gives you a ready-made table to paste. See SOQL query merge fields in Excel.

Limits and gotchas

  • Scope every query. A query or Apex class without a record filter returns records from across the org, not just this record's.
  • Query errors come from Salesforce. A stray : or wrong quotes gives DME-SF-001, a wrong or unreadable field DME-SF-002, and a wrong or unreadable object DME-SF-003. Test the query in the Developer Console with a real record ID.
  • IDs differ between orgs. A QVar0Id or template ID copied in a sandbox points to a record that has a different ID in production. Check them after you migrate a solution.
  • Add only what you need. Each extra relationship slows generation.
  • If a table comes out empty or repeats rows, see Table rows are empty or repeating.