You can create a method in Apex which will return the data required in your document template. To use that type of data source, an Admin should create a class with TemplateApexDataProvider interface implementation. Interface includes two methods:

  • getAvailableTokensWithLabels – method defines Aveneer Token names and labels that are to be displayed as picklist values under the Configuration section of a Template Configuration record when Method mapping is selected.
  • getDataSource(TemplateApexDataProviderContext context) – method retrieves data from the database and assigns them to appropriate Aveneer Tokens.

After the correct class is created, it is available for selection when the “Method” data source is selected on the Template Configuration record page under the Configuration section.

Sample code can be found below. Remember to change the variables to ones that will suit your needs. To implement the code go to Setup -> Custom Code -> Apex Classes and create a new Apex Class.

global class SampleDataProvider implements ave.TemplateApexDataProvider {

     global Map<String, ave.ApexDataProviderTokenLabelInfo> getAvailableTokensWithLabels() {
         Map<String, ave.ApexDataProviderTokenLabelInfo> example = new Map<String, ave.ApexDataProviderTokenLabelInfo>();

        // standard tokens, string which we put in key should match key in getDataSource method
        example.put('phone', new ave.ApexDataProviderTokenLabelInfo('Phone'));
        example.put('fax', new ave.ApexDataProviderTokenLabelInfo('Fax'));
        example.put('name', new ave.ApexDataProviderTokenLabelInfo('Name'));

        // table token, mostly used in case, when we need to display some child records for example some data from Contacts related to Account
        ave.ApexDataProviderTokenLabelInfo table = new ave.ApexDataProviderTokenLabelInfo('Account Contacts');
        table.addChild('first_name', 'First Name');
        table.addChild('last_name', 'Last Name');
        table.addChild('phone', 'Phone');
        example.put('contacts', table);

        return example;
    }

    // ave.TemplateApexDataProviderContext context - generation context with two useful methods:
    // - List<String> getRecordIds() - return list of recordIDs to generate
    global ave.DataSourceWrapper getDataSource(ave.TemplateApexDataProviderContext context){
        ave.DataSourceWrapper dataSource = new ave.DataSourceWrapper();

        for(String recordId : context.getRecordIds()){
            dataSource.addForRecord(recordId)
                    .addValue('phone', '123123')
                    .addValue('fax', '321321')
                    .addValue('name', 'Test Account');
            for(Integer i = 1; i <= 3; i++) {
                dataSource.addForRecord(recordId)
                        .addTableRecord('contacts',
                            new ave.DataSourceRecordWrapper()
                            .addValue('first_name', 'First Name ' + i)
                            .addValue('last_name', 'Last Name ' + i)
                            .addValue('phone', String.valueOf(i))
                );
            }
        }
       return dataSource;
    }
}