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

  1. Click the gear icon and select Setup.

  2. In the Quick Find box, enter Apex Classes, then click Apex Classes under Custom Code.

  3. Click New. The Apex class editor opens.

  4. Write a class with a meaningful name that implements hic_docmerge.IDmeApexDataSourceProvider. The class must return your datasets as a Map<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>>();
        }
    
    }
    
  5. Click Save.

Register the Apex data in Docs Made Easy

  1. From the App Launcher, open Docs Made Easy and create a document solution.

  2. Go to Gather Data and click New Apex Data. The New Apex Data window opens.

  3. Select your Apex class, enter an Alias for the Apex data and click Save. The Apex data is listed on the Gather Data page.

    Alias field for the Apex data and the Save button

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

    Add Templates step with options to upload a document and open the Merge Field Helper

  5. 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:

Generated document with Account Details and Contact Details tables filled from Apex data

The opportunities dataset appears as its own table:

Generated document with an Opportunity Details table filled from Apex data

Filter records by record ID

To return only records related to the record you generate the document from, filter by record ID.

  1. 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
    ];
    
  2. In the solution's Assign Behaviors step, click Other Parameter. The Edit Other Parameter window opens.

  3. Add a parameter that passes the record ID. In this example, the parameter name is :recordid and the value is {!Account.Id}.

    Edit Other Parameter window with :recordid set to {!Account.Id}

Docs Made Easy now fetches only the records that match the condition. The generated document:

Generated document showing only the current account and its related records

The values match the account record in Salesforce.

Related: Use SOQL queries in Docs Made Easy · Release notes v1.25