Word DOCX Content Template
This article demonstrates how to use a Word content template with Word content commands.
Creating a Word Content template
Any Word DOCX document can be used as a template. What will turn the document into a Dynamo template are the content controls with Word content commands in them. So let's create one. The first step is adding in a content control. This is done through the Developer tab's Controls group. The Developer tab might not be visible in the Ribbon, in which case head to Word's options and use the Customize Ribbon page to add the tab.
In this example we're adding some dynamic text, so click the "Aa" icon - shown in the image below - to add a rich text content control into the document. If you have a content selection active at the time of pressing the content control icon, the content control will wrap around that content, otherwise it will appear as an empty control at the position of your text cursor. This content control may contain any placeholder text - it will be replaced by a variable value during the evaluation process.
With the content control in place, the next step it to add a Word content command into it. Click anywhere within the content control and then click Properties in the Ribbon's Controls group.
This brings up a dialog for the content control's properties. The two text fields at the top, Title and Tag, are what we're editing here:
First, type "dynamo" into the Tag field. This marks the content control as something the evaluation process should be interested in.
Next is the Title, which will contain the Word content command. We can use the out command here, which will set the value of a variable as the text content of the content control. In our example, to display the Opportunity account name, the Title section of the Content Control Properties should be
out(opp.Account.Name)
.
And that's it, now this Word document could be called a template as it contains a Word content command. Upload this template into your Salesforce organization so that your application's Flow can then access and compose it.
Minimal Logic Example
To do the composing, the command composeDOCXContent needs to be used. If you're familiar with how HTML template files get composed in the logic, this is exactly the same thing except only with a different command. There are 2 mandatory steps involved to get your DOCX content template composed in Dynamo:
Load the Word file into the logic: after uploading the DOCX file (the content template) to Salesforce, take the Id of the file and use it in the loadLatestContentVersion command. In our example, the variable that holds the loaded file is wordFile.
Compose the DOCX content template using command composeDOCXContent, with the variable generated in the previous step (wordFile) used as input for the command's template attribute. The output of the command is a composed DOCX document, stored under the variable outputFile in this case.
Using Tables
A common element in legacy DOCX templates was a dynamic table and it can be present in these DOCX templates as well. The table command exists for this, and can be used together with out commands to create a dynamic table with text contents. In this example, we'll create a table listing some field values of a set of Salesforce records.
Preparing table data
Before creating the table, the data to be displayed should be fetched via data retrieval commands in a logic step before the composing command composeDOCXContent.
In the example template (downloadable at the end of the tutorial), the data retrieval commands below are placed in step 'Salesforce data'. Data needed for the product line item table is fetched via command relatedList under the variable products. This variable will be used in the table content command later.
Using 'table' command
With the data ready, the table for it can be created. Please follow the following steps to add a dynamic table:
Insert a table into your document - give it one or two rows and as many cells on each row as there are field values per record you'd like to display.
This table needs to then be wrapped by a content control:
Select the table (easily done by clicking anywhere on the table, and then clicking the rectangular icon at the table's top-left corner)
Click the rich text content control icon in the Developer tab on the Ribbon.
Add the table command into the Content control properties' Title field. The table command requires two attribute values, value and var, in that order. The first of these, value, will be the variable containing the Salesforce data, which in our case "products". The second attribute var is iteration variable that holds the current item of the loop, which in this case can be i. With these, the Title of the content control would be
table(products,i)
. Also remember to set the Tag todynamo
.
Setting table's header
From the Design ribbon, you can set the table style to one that defines the header row. The header row will not be multiplied by the table command.
Mapping Salesforce fields into table row
The second row, or the only row in a single-row table, will be the template for displaying data of the records. The table command will multiply this row so that there will be one row for every record. Add a rich text content control into each of this row's cells and set their Titles to have out commands referring to the fields of internal "i" variable representing the data of a single record. In our example, as each row displays fields from a product line item, the input for Title area for each Content Control properties box is under the form out(i.lineItemField)
.
With the row's content controls in place, the dynamic table is ready. Compose the template and see if you'll get something resembling the table below.
Example Template Files
Dynamo Online template using Word content template: WordContentTemplateExample.dap
Word content template: WordContentTemplateExampleTest.docx