Thursday, 11 June 2020

PDF Automation series - Part 3: How to automate generating a PDF and attach to an outbound email

The final of my PDF Automation series covers how to use the custom connector for Dynamics 365 to automate generating a PDF and attach to an outbound email. Refer to the following as a prerequisite to what the previous two WTF episodes covered which will be reference in this finale of my PDF Automation series.
**I'd like to make it clear that as of today, Convert to PDF is configured and supported in the Dynamics 365 Sales Hub app**

In this WTF episode I will cover the flow in Power Automate that references the Document Template record from Part 1 and the custom connector for Dynamics 365 from Part 2.

Use Case

Before we jump into the How To's, a reminder that the use case for this series will be

As a customer,

I want to receive a Tax Receipt of my purchase in an email,

so that I have confirmation of my purchase.

The life cycle of the process will be
  1. A Dynamics 365 Invoice record is updated to paid.
  2. My flow in Power Automate is triggered that handles all the magic.
  3. The customer associated to the Invoice will receive an email with a PDF attached that represents their Tax Receipt.

What my flow in Power Automate looks like

My flow uses the custom connector and both actions that you would have read/seen in Part 2: How to create a custom connector for Dynamics 365. The flow is created in a solution and I'm using the CDS current environment connector.

1.0 Trigger - When an Invoice is updated to Paid

The trigger is when the status reason is Updated to Paid where the end user in the Dynamics 365 Sales Hub App clicks on Invoice Paid in the ribbon. 

I have also configured two filters for the trigger
  1. Only trigger if the Status Reason field has been updated
  2. And if the Status Reason field equals Paid

1.1 Retrieve queues

Using the CDS List records action I am retrieving a Queue with the name of "No Reply" as defined in my Filter Query field. To learn more about the configuration required to be able to send from a Queue, refer to Episode 23 -  Sending an email from a Queue with Power Automate.

1.2 Create a new email activity record

Using the CDS Create a record action an email activity will be created where the sender will be the Queue from 1.1 Retrieve queues and the To recipient is the customer of the Invoice.

I've cut out area of the action as some it is not used otherwise it would have been a long screenshot 😅 The other configuration is providing a Description, setting the Regarding value to the Invoice and the Subject of the email.

1.3 Retrieve Document Template record

Using the CDS List records action I am retrieving a Document Template record where I had uploaded a Word Template I created. This would be Document Template record from Part 1. This will be the template used to generate the PDF for an a paid Invoice where it includes information such as the customer, the bill to address, the invoice items and so forth.

The filter query is the name of the Document Template record.

1.4 Generate PDF

Woo hoo! This is where the action created in the custom connector is used. The Generate PDF action is used where the values are provided for the JSON payload of the API request. I covered this in Part 2.
  • The EntityTypeCode represents the entity that the PDF will be generated against. In my use case it is the Invoice entity where the value is 1090.
  • The @odata.type is Microsoft.Dynamics.CRM.documenttemplate
  • The documenttemplateid is the GUID of a Document Template record. Refer to Part 1. My expression since the CDS List record action is used to retrieve the Document Template record is the following to avoid the Apply to Each from appearing
    • outputs('1.3_Retrieve_Document_Template_record')?['body/value'][0]?['documenttemplateid']
  • The SelectedRecords property is the GUID of the Invoice record from the trigger that the PDF will be generated against. I included the additional characters the API request expects which I explained in Part 2.

1.5 Create Email Attachment

The other custom action used is creating an activitymimeattachment record by 
  • binding to an email message which is from 1.2 Create a new email activity record action
  • referencing "email" as the objecttypecode
  • using the base64 PdfFile property in the JSON response of 1.4 Generate PDF action
  • using the Invoice ID from the trigger as the file name and subject
    • note that for the file name ".pdf" is used as the file format

1.6 Perform a bound action

The last step is the CDS Perform a bound action using the SendEmail action. Simply reference the email message from 1.2 Create a new email activity record and set the IssueSend value to Yes.

Power Automate in action

Update an existing Invoice to Paid and refresh to see the email activity created with a PDF that represents a Tax Receipt.

Unit testing

The following are screenshots to confirm the attachment record was correctly created. I mentioned in Part 2 that I learnt from this forum post of how the activitymimeattachment should be created first where Dynamics 365 will then create the corresponding attachment record.

This was the Invoice that was updated to Paid.

Here's the activitymimeattachment response from the custom connector. The activitymimeattachment was successfully created.

I queried the activitymimeattachment API to retrieve the record details. 
  1. The activitymimeattachmentid matches the id from the response.
  2. I can see the corresponding attachment record was created too.
  3. I can also see the activitymimeattachment record was binded to the created email activity.

I queried the attachment API to retrieve the record details. I can see the attachmentid matches the property value from the activitymimeattachment record and the filename and subject matches the Invoice ID.

Lastly, I queried the emails API to retrieve the record details. I can see that the activityid value matches the _objectid_value from the activitymimeattachment and that the Invoice ID referenced in the email subject matches the paid Invoice. It all ties together nicely 👍

Another handy blog post

Another blog post you can refer to that has a different flow in Power Automate but achieves the same result is by another Microsoft MVP in our community, Aric Levin which you can read here. What I like about Aric's blog post is that it would be an option for customers who use CDS but not Dynamics 365 as the Convert to PDF functionality as of today resides in the Dynamics 365 Sales Hub app. Aric also uses the Office 365 action to send the email with the generated PDF file which means the storage consumption of CDS will not be impacted. The exception is if the App for Outlook is installed and tracking is enabled which means the sent email from Outlook may be created as an email activity based on the configuration of tracked emails for the user.


Automating generating PDFs from the Dynamics 365 Sales Hub app is achievable with Power Automate through
  1. A Word Template uploaded against a Document Template record
  2. A custom connector for Dynamics 365 that calls the ExportPdfDocument API and the activitymimeattachment API
  3. A flow that references the Document Template from No.1 and use the custom actions created in the custom connector for Dynamics 365 from No.2
I hope you enjoyed my PDF Automation series 🙂

Your Vote Counts!

If you want Convert to PDF across custom entities then vote for this Dynamics 365 Idea to gain momentum. I would love to see this functionality across the Dynamics 365 platform so let's do our part by voting for it.


Where my Starship Troopers fans at?! 😁


Post a comment