Power Automate: Send an email using the Email Template – Part 1

Sending
User Review
0 (0 votes)

Introduction

What options do we have for sending an email using the email template on specific events/actions in Dynamics CRM?

We have a classic workflow with ‘Send Email’ and ‘Use Template’ action or a Plugin with ‘InstantiateTemplate’ and ‘SendEmail’ request for this. Now we can do the same thing using Power Automate as well.

This blog will illustrate how we can create a Power Automate Flow to send an email using the Email Template.

Let us consider a scenario: a company has an inquiry form on their website. On each submission, a new lead gets created in the CRM and for each newly created lead, an email with a thank you message should be sent to the customer.

Setup trigger for Power Automate

We will create a Power Automate Flow that will get triggered on the creation of the Leads record. For this, we will select ‘When a record is created, updated or deleted’ trigger and will select ‘Create’ as the trigger condition. If you want to learn more about scope, refer to this blog.

Retrieve Email template

We will add ‘List Records’ step to retrieve the Email Template by its name (Title). We are retrieving the below template.

Add a filter query to retrieve template by the title as shown below

Send an email using the Email Template in Power Automate

This step will return the ‘Lead Reply-Web Site Visit’ template. We will use the GUID of this template in the next step.

Use ‘SendEmailFromTemplate’ action

This action is used to send an email using the Email Template. To execute this in Power Automate we need to add ‘Perform an unbound action’ step.

For this step/action, we need to pass the below parameters,

Action Name: Select SendEmailFromTemplate’ from the list of actions.

TemplateId: GUID of the Email Template. In the second step, we retrieved the email template, we can use following expression to get the GUID of the template.

first(body(‘List_records’)?[‘value’])?[‘templateid’]

Regarding: This is the Record to be set as the regarding of the email. Here we will pass the newly created Lead as the regarding. Pass the record as the following

/leads(triggerOutputs()?[body/leadid])

Target: This is the parameter where we need to pass the sender and recipients of the email in JSON format. Below is the JSON structure

{

“email_activity_parties”: [
{
“partyid_<entitylogicalname>@odata.bind”: “/<entitysetname>(<guid of the record>)”,
“participationtypemask”: 1
},
{
“partyid_<entitylogicalname>@odata.bind”: “/<entitysetname>(<guid of the record>)”,
“participationtypemask”: 2
}
],
“@@odata.type”: “Microsoft.Dynamics.CRM.email”

}

It is important to understand the Target parameter’s JSON structure because this is the place where we set TO, CC, BCC and FROM for the email.

In the above JSON, I have used ‘participationtypemask’ as 1 and 2. Use 1 to set FROM and use 2 to set ‘To’.

If you want to set CC and BCC then use 3 and 4 as ‘participationtypemask’ respectively.

Send an email using the Email Template in Power Automate 1

This way ‘SendEmailFromTemplate’ action will allow you to send an email using the Email Template. Subject and email body will come from the email template and Sender, Recipients (To, CC, BCC) will be set based on the Target Parameter.

Below is my “Perform an unbound action” and its JSON

Send an email using the Email Template in Power Automate

TemplateId is the GUID of the template retrieved from the second ‘List Records’ step, ‘Regarding’ is the entity object of the lead that is being created and in the Target parameter, ‘FROM’ is being set to Owner of the lead and ‘To’ is set to the lead entity object.

{
“email_activity_parties”: [
{
“partyid_systemuser@odata.bind”: “/systemusers(@{triggerOutputs()?[‘body/_ownerid_value’]})”,
“participationtypemask”: 1
},
{
“partyid_lead@odata.bind”: “/leads(@{triggerOutputs()?[‘body/leadid’]})”,
“participationtypemask”: 2
},

{
“partyid_systemuser@odata.bind”: “/systemusers(c308eef1-1de7-ea11-a817-000d3a0a82a5)”,
“participationtypemask”: 3
},
{
“addressused”: “sam@test.com”,
“participationtypemask”: 4
},

{
“addressused”: “john@test.com”,
“participationtypemask”: 4
}

],
“@@odata.type”: “Microsoft.Dynamics.CRM.email”
}

Sometimes, you may want to send an email to the email address which doesn’t exist in your system. In that case, we can use the ‘addressused’ parameter. In the above code snippet, you can see I have set sam@test.com for BCC.

In addition, you may want to send more than one recipient in either TO, CC or in BCC. It is possible to set multiple objects in TO, CC and in BCC. You just need to add multiple entries in the ‘email_activity_parties’.

Below is how my final working Power Automate Flow looks like

Send an email using the Email Template in Power Automate

Below is the screenshot of the newly generated mail using the Email Template

Send an email using the Email Template in Power Automate

Note: If any of your recipients is either a contact, lead or account and is marked as do not allow for Email then the flow will fail with an error saying, “Recipient of type ‘Lead’ with ID ‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’ is marked as non-Emailable”

Send an email using the Email Template in Power Automate

Conclusion

We can now consider Power Automate Flow to send automated email using the Email Template using “SendEmailFromTemplate” action. In the Part 2 blog, we will see how to send bulk email messages using Power Automate.

Reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/sendemailfromtemplate?view=dynamics-ce-odata-9