Invoke D365 OTB word templates from Power Automate

One of the nice features available out of the box is the ability to generate Word Templates in an easy way while using customer engagement apps (Dynamics 365 Sales, Dynamics 365 Customer Service, Dynamics 365 Field Service, Dynamics 365 Marketing, and Dynamics 365 Project Service Automation).

But not usually you need to generate or use the word template through the UI but you may need to generate it through a Process/ Flow / Plugin or even JavaScript, so while digging into this rabbit hole and trying to find out how this can be done , I found an Existing OTB custom workflow activity that does the Job by generating the template and attach it as a note against the record, by creating a new Process of Type Action and Calling this OTB custom workflow activity , you can call this action from anywhere Process/ Flow/ Plugin to get your template generated and then you can use it to send it as an email for example.

For the purpose of this article we will be using the Invoice out of the box Template and using it to send out email with the generated word template.

Please follow the below steps to get this working:

  1. Create a new solution.
  2. Create a new Process , Set Process Name to Action: Set Work Template, Set Category to Action and Entity to Invoice and click ok

3. Click on Add Step -> Perform Action

4. Choose the Action SetWordTemplate

5. Click on Properties, Choose the Selected Template and the Target and click Save and close

6. Click on Activate and Activate your Business Process

7. To call the action from your power automate , Choose Perform a bound action on the CDS Current Environment Current

Choose the Table to be Invoices and Select the Action Name as below and pass the Invoice ID

8. To get the generated document as mentioned above this has created a note against the invoice record so in order to get the generated note record against the invoice.

Add a List Rows Action with the below FetchXML:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="annotation">
    <attribute name="subject" />
    <attribute name="notetext" />
    <attribute name="filename" />
    <attribute name="annotationid" />
    <order attribute="createdon" descending="false" />
    <filter type="and">
      <condition attribute="isdocument" operator="eq" value="1" />
      <condition attribute="filename" operator="eq" value="Invoice.docx"/>
    </filter>
    <link-entity name="invoice" from="invoiceid" to="objectid" link-type="inner" alias="ab">
      <filter type="and">
        <condition attribute="invoiceid" operator="equitype="invoice" value="Invoice ID" />
      </filter>
    </link-entity>
  </entity>
</fetch>

You will need to Replace the below part in the above fetch with your regarding entity for the note which in my example is invoice, in other scenarios it can be contact or account.



   <link-entity name="invoice" from="invoiceid" to="objectid" link-type="inner" alias="ab">
      <filter type="and">
        <condition attribute="invoiceid" operator="equitype="invoice" value="Invoice ID" />
      </filter>
    </link-entity>

Also replace the below filter condition with the template name .docx

 <condition attribute="filename" operator="eq" value="Invoice.docx" />

9. This is sorted desc by created on , so getting the first record on the list gets the last generated document.

10. Next we need to get the GUID first record from List Rows, so use a compose data operation with the below expression:

first(outputs('List_rows')?['body/value'])['annotationid']

11. Use the Get a row by Id and pass the outputs from the compose data operation to the ROW ID

12. Add a new send email step, Pass the file name and the Document properties from the Get a row by ID in step 11 , For the document to be displayed correctly in the email you need to use the expression below for the Attachment Content and not directly pass the document.

Expression:

base64ToBinary(outputs('Get_a_row_by_ID')?['body/documentbody'])

References:

https://docs.microsoft.com/en-us/power-platform/admin/using-word-templates-dynamics-365

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 )

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

Create your website with WordPress.com
Get started
%d bloggers like this: