Convert Word to PDF in Dynamics 365 CE Online with Flow (Part 2)

I discussed about advantages and disadvantages of different approaches in part 1. I’ll cover how we can achieve our goal with Flow in this part.

We have setup the workflow to generate Word document in this post. Now we need to create to convert the Word document to PDF document and attach the PDF to the Note. There is the OneDrive connector with a lot of actions which help you work with files in and the most important in our case is the “Convert file” to PDF action.

First you need to setup a trigger to run the Flow, there are 2 ways we can consider:

1. Dynamics 365 trigger

We can use a D365 trigger when a Note record is created CRM, then:

  • We check if the Note has attachment and relates to a record of type “Student”, if true
  • We convert the Word document to PDF, then
  • Create a new Note record with PDF attachment and attach the Note to the parent Student record.

Here we are using Dynamic 365 connector for the trigger, and the creation of the new PDF Note. We need to specify a target organization for the D365 connector to connect to. So we are sticking our Flow with only one organization. And we have to create multiple Flows with the same functionality for different organizations/environments such as dev, sandbox or prod. This may introduce problems with deployment and maintenance.

If  you find it achievable plus you don’t have to write any code, I’m sure you can setup a Flow with above logic. Everything is straightforward with conditions and actions just like CRM workflow.

However, please note that the number of requests to Flow are limited depending on service plan. With free plan you only have 2000 requests per month. In this approach the flow is triggered whenever a Note created and it consumes Flow request every time. So to save the requests and to be more generic to apply to multiple organizations, I’m going with the second approach and explain detailed steps.

2. Service-like Flow

We’ll create a Flow that is triggered on request, convert the Word document and return the result. It looks like a web API. We only use it on demand whenever we need and we don’t bind it to any organization.

Our Flow should look like below

convertflow.PNG

When a HTTP request is received

To add this step, search for “Request” connector > select “Request – When a HTTP request is received” action

request.PNG

The HTTP POST URL is generated automatically after you save the Flow. It contains security token, so be careful when you share this URL.

In this step, we need to define the Request Body JSON Schema which is the data structure in the request body we send to the URL. This will helps us to reference to the properties in the object in later step. You can see that we will send an JSON object which contains:

  • “filename”: this is the name of the document
  • “base64data”: this is the document data in base64 string. You may know that all attachment/document data in CRM are stored as base64 string.

Create Word file

To add this step, search for “OneDrive” connector > select “Create file” action.

createwordfile

We need to save the document to OneDrive, so we can pass the document to conversion in the next step.

In the first property, we set the File Content to the base64data in the JSON object. However, OneDrive stores data as binary so we need to convert the base64data to binary using following expression:

base64ToBinary(triggerBody()?['base64data'])

Then we set where to save the file on OneDrive. I created a Temp folder to temporarily store the Word document and delete the document after conversion, so we can save some storage space in OneDrive.

Next we set the file name to use the filename from the JSON object. And we add a prefix of current UTC date time to the file name with utcNow() expression so that we can prevent duplicate file name which may cause error.

Convert to PDF

To add this step, search for “OneDrive” connector > select “Convert file” action.

converttopdf.PNG

Set the Target type to PDF, and the File to the ID of the document created in the previous step.

This step will return a PDF document but it is not saved in OneDrive and we don’t need to save it. We just need to read it’s content.

Response – return PDF

To add this step, search for “Request” connector > select “Response” action.

requestresponse.PNG

In the Body, we will return the PDF data and we need to convert it to base64 string with following expression:

base64(body('Convert_to_PDF'))

Delete file

To add this step, search for “OneDrive” connector > select “Delete file” action.

deletefile.PNG

We pass in the ID of the Word document created in previous step.

Click Create flow to save the Flow, then click Done to close the editor.

Now we have the PDF conversion flow ready. In the next part, we’ll create a custom workflow to send Word document request to this Flow and receive a PDF in result. Then test it in CRM to see it in action.

3 thoughts on “Convert Word to PDF in Dynamics 365 CE Online with Flow (Part 2)

  1. Hello! I going through the steps you outlined and there is missing information. On the step of “Response – return PDF”, the Flow I’m using is asking for a “Response body JSON schema”. What do I put in this area?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s