Our Blog



Make your Dynamics templates shine with Microsoft Flow

A common request from clients when implementing Dynamics 365 for Customer Engagement (CE) is to produce beautifully formatted documents such as contracts, letters and application forms. These documents need to be accurate and contain all the required information, but also need to be presented professionally and align with your business and your brand.

Dynamics 365 CE out-of-the-box has some limitations when producing these documents through the "Word templates" option, such as:

  • You are unable to filter records
  • You are unable to sort records
  • You are unable to include data that is more than one relationship away (“double hop”)
  • There is no provision for conditional criteria (“IF” conditions)
  • You must manually convert the Word documents to produce a PDF
  • Every time you make a configuration change to any of the fields that are used on your template, you must recreate the Word template and the mapping
  • If you are referencing custom entities in your template, then you are unable to use the same template between environments – you must recreate your templates in each environment, as opposed to using the deployment process.

With Microsoft Flow and the Common Data Service, you can address these limitations and do more, much more!

To demonstrate, we are going to walk through how to do the following:

  1. Create your Word template
  2. Trigger the document creation from Dynamics 365 CE
  3. Extract data from Dynamics 365 CE (multiple entities and an entity that is twice removed)
  4. Map your data to the Word Document

Please note that the Microsoft Word connector that we will be using is currently in preview – meaning that it may still change and that you might have to replace it once the permanent connector is released. It’s also important to note that we will be using some premium connectors in Flow – make sure that your licensing allows you to use them.

Now that the formalities are out of the way, let’s get our hands dirty! 

Scenario: I want to create a “service agreement” document that I can send to my client, containing the following details:

  1. The Client I have selected (Account entity)
  2. Name and email address of the Primary Contact (Contact entity);
  3. List of the Entitlements (Entitlement entity);
  4. List of the Entitlement Channels for all the Entitlements (Entitlement Channel entity);
  5. Include a conditional sentence, depending on the number of employees the Client has.

Step 1: Create your Word template

Open a new Word document and create your template with all the required fields, tables and formatting.

Add content controls where you will be populating data with your Flow.

The Controls you need is under the Developer menu option (if you are unable to see this option on Word then you will need to enable it by going to File --> Options --> Customize Ribbon --> Select the Developer menu option under the Main Tabs section).

We will be using these options on the Developer menu:

  • Plain Text Content Control
  • Repeating Section Content Control
  • Control Properties

To create a single data field:

  • Place your cursor where you want to add the field and select Plain Text Content Control (1).
  • Click on Properties and give the Control a name. Make note of this name as you will be using it in Flow to map the data to the specific field on your template. In the example below, I have called my field “ConditionalText”.

To create repeating rows in a table:

  • Create each field in the row individually (like I did in the previous step).
  • Highlight the whole row (make sure to exclude the header row) and select the Repeating Section Content Control (2). Once this is done you will see a “+” (plus) sign next to your row.
  • Click on Properties and give the Repeating Control a name. In the example below, I have called my repeating row “Channels”.

Once done, my template looks as follows:

Now, save your template on SharePoint.

Step 2: Draw a quick entity diagram

To help you keep track of the entities and relationships involved, it will be useful to draw a quick entity diagram like this one:

Step 3: Trigger the document creation from Dynamics 365 CE

In Dynamics 365 CE, open a record from your source entity (in my example the source entity is an Account). On the menu, select Flow and Create a flow. This will open Microsoft Flow and create the trigger step.

Step 4: Create your Flow

This is where everything comes together. In summary this is what the Flow looks like:

Let me take you through the detail:

This step will be created automatically when you select to create a new Flow from Dynamics 365 CE.

For each table that will contain multiple rows you will have to create an Array Variable – at this point we are only creating the variables by using the function “Initialize variable” and setting the property Type as Array.

Creating a second Array Variable for Channels.

Extracting the Account fields for the current Account record.

Extracting the fields from the Contact entity for the Account’s Primary Contact.

Extracting all the Entitlement records for the selected Account.

Tip! you can use FetchXML Builder from XrmToolBox to help you build your Filter Queries.

For each of the Entitlement records extracted in the previous step we will be applying some steps too. 

To create each row in the table we will use the function “Append to array variable”. This is where we map the columns in the Word template to the data fields we have extracted. Important! The field names you enter here must match exactly the names you gave the fields on your Word template’s Content Controls. If they are not an exact match the fields will be blank.

For each of the Entitlement records we have selected in the previous step we want to extract the Entitlement Channels.

Now we will create the rows for the Entitlement Channel table by populating the Array Variable.

Again, we are mapping the row’s fields in our table to the fields we extracted from Dynamics 365 CE.

We want to add conditional text on our template. Our condition:

IF [number of employees] < 10,000
THEN “Thank you for your support.”
ELSE “Thank you for your support and for being a key strategic partner.”

This is what the formula in our expression looks like:

if(less(triggerBody()?['entity']?['numberofemployees'],10000),'Thank you for your support.','Thank you for your support and for being a key strategic partner.')

Now that we have all the data, we can populate the Word template with either a field or a variable. Where we populate a Repeating Section Content Control on a table, the name of the row will have a dotted border around the repeating section name to indicate it contains multiple fields (e.g. Channels and Entitlement). You will map these fields to your array variables.

The last step is to save our newly populated Word template.

Step 5: Run the Flow

Our result after running the Flow.

The conditional field that we populated is showing the second sentence, seeing as the company has 12,000 employees (i.e. greater than my 10,000 rule).

I hope the above examples show you the ease and flexibility with which you can populate documents with your Dynamics 365 data. This example only touches on the absolute basics – additional features you can add to this process reach far beyond this scenario.

Let’s create some incredible documents!

Talk to us if you need some help to create some amazing documents for your customers. We will help you plan your templates and extract your data to produce personalised documents.

Posted by: Bertus Malan, Consultant | 01 October 2019

Tags: Dynamics 365, Microsoft Flow

Top Rated Posts

Blog archive

Stay up to date with all insights from the Empired blog