Contact us Blog

Using multiple templates for document generation


When generating Word or PDF documents from Salesforce data, the most common need is to generate one or more documents from a single template, replacing the place-holders in the template with data from Salesforce. This would apply, for instance, when generating an invoice, a proposal or a thank-you letter. Apsona's document generator provides full support for this case, as documented elsewhere.

But there are cases where you would want to use multiple input templates, and have the system select the one to use based upon the data being used to generate the document. For example, suppose you wish to generate acknowledgement letters for the contacts who participated in your school fundraiser, but the template varies depending on whether the contact is a parent, a student, a faculty member or a member of the school board. You would still want to generate (in one batch) the letters for all the participants, but you would want the system to select the appropriate letter style and content, depending on the type of contact.

Here are a couple of approaches to address this situation.

Approach 1: multiple conditionals

If you had a common header and footer for all the different letter types, and you just want to vary the body of the letter depending on the contact, you could create a single template that conditionally generates the appropriate letter body depending on the contact type. You can use Apsona's IF-THEN directives for this purpose, via a large IF-THEN-ELSE structure that checks the letter type. Below is a depiction of such a template.

Approach 2: conditional templates

For the more sophisticated cases, Apsona supports for "conditional templates" - one or more additional templates, along with the criterion to use when selecting each of those templates. The panel in the right part of the screen shot below shows the fields you need to specify when using conditional templates. Specifically, you provide: In the screen shot above, we have selected the Record type field as the condition field, so that field's value is used to select the template. Therefore, the template selection logic will be applied for each data record being used for the merge, as follows:

When you have many conditions

The screen space for working with conditional templates is limited, in that it shows about five rows at a time. And this is inadequate when you have lots of different conditions. In these cases, you can use the pop-out icon in the conditional templates panel so that it uses more screen space. See the animation below.

Notes on Approach 2