Generate Document Templates in CDS using Flow

Generate Document Templates in CDS using Flow

One of the features in Microsoft Dynamics 365 (and some of the previous versions has been document templates). I wanted to validate some of the logic, if we can automate the creation of a document and sending it as an email using Microsoft Flow, and of course Flow did not fail to perform.

In order to implement this logic, the first thing was to create the actual document in Microsoft Word. This document will be the template that we create, and will convert it to an Adobe PDF and finally Send it in an email message using Office 365. This process is based on a requirement to generate a license and send it as an email to the user when a new license is generated (or updated).

I found a sample template that I could customize in word. I designed the form, and at every place where I wanted Flow to insert a field, I created a Plain Text Content Control, and named it accordingly. The image below shows the completed design of the document in Microsoft Word.

Microsoft Word Document Template

After the document was generated, I uploaded the document to the SharePoint library to be used as a template. This is where flow will retrieve and generate the document from.

My flow starts with the Common Data Service (current environment) connector, using the Create or Update Trigger condition on the Licenses entity. I added an attribute on the Filtering Attributes, so that when the expiration date is modified, the flow will run and generate a new license and send it to the customer.

Common Data Service (current environment) trigger

I then created a Parallel Branch to get the details from the Account and Contact records so that I can use that data in the generated email template, as this is a license that contains the information of the Customer and has individual contact details.

Parallel Branch

I also needed to get the details of the product record, so I added one more CDS action to Get a record on the Product entity.

CDS Get Product Record

In all of the last three actions that I used, I specified the Select Query details to only get the fields that I needed from CDS, as well as limit the amount of data that is visible in Dynamics Content. This makes it easier to find your data.

Now that I have all the data that I need to fill in the template, I will use the Populate a Microsoft Word template action to add all the data to the Word document.

Flow Populate a Microsoft Word template

The word document has three separate fields for the Print Date (Day, Month, Year), so I use the following function to display the partial date in each field:

convertTimeZone(utcnow(),’UTC’,’Eastern Standard Time’,'{FORMAT}’)

Since SharePoint does not have an PDF converter yet (or at least one that is free to use), I used the one that is available in OneDrive for Business to Create and Convert the file to PDF. First we will save the file in a OneDrive folder as shown in the image below, using the Create file on OneDrive action.

Flow Create file on OneDrive

Next, we will convert the file to a pdf using the Convert file to PDF action. The only required parameters are the File Id from the previous step and the Target type which is PDF.

Flow Convert file to PDF (OneDrive)

We can then store the generate file back into SharePoint (as a repository), but this step is not necessary. If we still want to do this, we will pass the SharePoint information, path, filename and the content of the file to save.

Flow Create file in SharePoint (Optional)

The next step is to Send the file. There are various ways to achieve this, and the easiest one is to call the Send an Email (V2) action, which will Send it from Office 365, as shown in the screenshot below.

Flow Send an email

I use the Email field from the Contact record, generate the subject and description with information from the license agreement, and use the OneDrive generated PDF and filename to add as an attachment.

Alternatively, if you would like to use the Email Entity in CDS and Server Side Sync to send the email, that is possible as well. You can get the Sender information from the Users entity (or if you have it in the license record even better). You then have to create an Email record with the details of the email, just as above. If you want to send the attachment with the email, you can create an attachment record and set the activity of the attachment record to the newly created email record.

You would then perform a bound action on the Email Message activity, selecting SendEmail as the Action name and passing the Item id of the email message.

When we run the entire process, we see that the Flow ran successfully.

Testing the Flow execution

A few seconds later, I open my Outlook, and will see that the message has been delivered to my Outlook, with an attachment associated with it.

The final generated email in my Outlook box

If we open the attachment record, we will see the license with information and details populated within.

License Details generated document

Additional information about the new CDS connector is available on the Microsoft Docs website.

https://docs.microsoft.com/en-us/connectors/commondataserviceforapps/