An Apex data relationship lets you collect, process and structure data in an Apex class before Docs Made Easy merges it into a template. Use it when the data you need goes beyond standard SOQL-based relationships, for example dynamic, computed or conditional datasets.
Before you begin: You need permission to create Apex classes in Salesforce, and you should be comfortable writing and maintaining Apex code.
Create the Apex data class
Click the gear icon and select Setup.
In the Quick Find box, enter
Apex Classes, then click Apex Classes under Custom Code.Click New. The Apex class editor opens.
Write a class with a meaningful name that implements
hic_docmerge.IDmeApexDataSourceProvider. The class must return your datasets as aMap<String, Object>so Docs Made Easy can merge them into the generated document. For example:global class SampleDmeApex implements hic_docmerge.IDmeApexDataSourceProvider { /* * Primary method used by Docs Made Easy. * Fetch and return your datasets using a Map<String, Object>. */ global Map<String, Object> getData(String recordId, Map<String, Object> params) { Map<String, Object> data = new Map<String, Object>(); // Fetch Accounts List<Account> accounts = [ SELECT Id, Name, Industry FROM Account LIMIT 10 ]; // Fetch Contacts List<Contact> contacts = [ SELECT Id, FirstName, LastName, Email, Phone FROM Contact LIMIT 10 ]; // Fetch Opportunities List<Opportunity> opportunities = [ SELECT Id, Name, StageName, Amount, CloseDate FROM Opportunity LIMIT 10 ]; // Add datasets to the response map data.put('Accounts', accounts); data.put('Contacts', contacts); data.put('Opportunities', opportunities); return data; } /* * Reserved for future use by Docs Made Easy. */ global List<Map<Object, Object>> getData(String recordId) { return new List<Map<Object, Object>>(); } }Click Save.
Register the Apex data in Docs Made Easy
From the App Launcher, open Docs Made Easy and create a document solution.
Go to Gather Data and click New Apex Data. The New Apex Data window opens.
Select your Apex class, enter an Alias for the Apex data and click Save. The Apex data is listed on the Gather Data page.

Add the merge syntax for the Apex data to your template, then upload the template in the Add Templates step. You can get the syntax from the Merge Field Helper in that step.

Complete the remaining wizard steps to finish the solution. For details, see Create a template with the Document Automation Wizard.
The generated document shows the data returned by the class, for example account and contact tables:

The opportunities dataset appears as its own table:

Filter records by record ID
To return only records related to the record you generate the document from, filter by record ID.
In the Apex class, add a filter condition that uses
recordid. For example:// Fetch current Account List<Account> accounts = [ SELECT Id, Name, Industry FROM Account WHERE Id =: recordid LIMIT 5 ]; // Fetch related contacts List<Contact> contacts = [ SELECT Id, FirstName, LastName, Email, AccountId FROM Contact WHERE AccountId =: recordid LIMIT 5 ]; // Fetch related opportunities List<Opportunity> opportunities = [ SELECT Id, Name, StageName, CloseDate, AccountId, Amount FROM Opportunity WHERE AccountId =: recordid LIMIT 5 ];In the solution's Assign Behaviors step, click Other Parameter. The Edit Other Parameter window opens.
Add a parameter that passes the record ID. In this example, the parameter name is
:recordidand the value is{!Account.Id}.
Docs Made Easy now fetches only the records that match the condition. The generated document:

The values match the account record in Salesforce.
Related: Use SOQL queries in Docs Made Easy · Release notes v1.25