Thursday, 5 March 2020

How to send an email using an Email Template with Power Automate

Sending an email using an email template in Dynamics 365 and CDS is straight forward in a classic workflow. It's simply a send email step and applying the "Use Template" option. If you want to replicate it via flows in Power Automate, there is an action available called SendTemplate for the Common Data Service Web API.

This Part 1 WTF episode came about as a result of another Microsoft MVP in our community, Sara Lagerquist. She was trying to figure out how to send an email using an email template through Power Automate. Initially I thought it couldn't be done but then chances are if it can be done in a classic workflow, it CAN be done in Power Automate using a different method. We know that there are APIs available and Power Automate plays well with APIs so I investigated it further. At the same time another Microsoft MVP in our community Aung Khaing, who is more known as AK, also investigated and beat me to it. Well, what you see in Part 2 originates from mine and AK's investigation.

In this WTF episode I share what AK came across and also outline the constraint of this method.

What is an email template?

It's self explanatory - it's a template you create for an email and you can associate it to an email activity record in Dynamics 365 or CDS. I am referring to the native email functionality of Dynamics 365 and CDS, this is different to an Outlook email.

When you create an email template, the template type is defined which is the entity the email template is associated to. A list of out-of-the-box entities will be available for you to select when creating an email template.

Replicating sending an email using an Email Template in Power Automate

Use Case

I'm referring to a use case that is common across all organisations that have a customer service centre.

"As a customer,

I want to receive an email confirming my request has been created as a Case,

so that I have acknowledgement from Company ABC of my request."

An email is required to be automatically send to an individual who has engaged with the organisation. The email usually provides details of the request and a reference ID. 

What my flow in Power Automate looks like

It's straight forward (except for the constraint) in Power Automate as an unbound action is used where you provide the details required by the action. Not exactly the same steps as a classic workflow but it works.

1.0 Trigger - when a Case is created

In my Power Automate the trigger is when a Case is created. Whenever a Case is created, send an email to the contact associated to the account in the Customer field.

1.1 Perform an unbound action

The unbound action will only be available if you have created your Power Automate in a solution and you are selecting CDS actions from the CDS current environment connector.


If you don't create your Power Automate in a solution you won't be able to use this particular action so make sure it is in a solution.

The information required by this action are
  1. The action which is SendTemplate
  2. The ID of the email template. This can be found in the URL of your email template record (refer to vlog)
  3. The Sender which I've made as the Owner of the Case
  4. The Recipient which I've made as the Customer of the Case. If an account is the customer value, the primary contact will be the recipient.
The following are optional
  1. The regarding value can be left blank where the email will not be associated to a record.
  2. The deliveryprioritycode which I have set to 1 which represents Normal. By default if you leave this blank Power Automate will set it to 0 which is Low.

Side note

When using the CDS current environment connector, make sure you enclose your dynamic content value with brackets and reference the entity as plural. Sara Lagerquist has a blog post on this.

Power Automate in action

Good to go, trigger Power Automate and away it goes.


If you have attached a file to the email template, this will also be included in the email.

Constraint

Using the SendTemplate action is useful when you have a single recipient to send to. In the use case where you have multiple recipients and it can vary based on the scenario, this action is not valid because of how you need to explictly add the the recipient by clicking on "+ Add new item". Ideally the recipients should be added dynamically so that it's 100% automated.

Stay tuned cause I'll share what you need to do in Power Automate when you want to send an email using an email template to multiple recipients.

Summary

The SendTemplate unbound action can be used when you have created your Power Automate in a solution. 

I also want to point out that it respects the design principles of email templates in Dynamics 365 (or CDS). If you are sending an email where the recipient is not of the same entity as the defined template type of the email template, an error will be thrown in the Power Automate run history. For example, if you want to send an email template related to a Case, the recipient must be defined using the Case entity. If you try referencing the Contact entity an error will be thrown because of how the email template is for the Case entity and not the Contact entity.

Credit goes to AK and cheers to Sara's question that sparked all of this.

No comments:

Post a Comment