Create and Send Emails with Attachments in D365/Dataverse with Power Automate
Carrying on with my last post on Word Templates, I wanted to cover how to take a Word Template, or any file for that matter, create an email and add this as an attachment. With Power Automate, you can connect to over 300 connectors, including the Outlook connector that can send emails, which is great. However, if you are used to working with Dynamics 365 or Dataverse, one of the key advantages is tracking emails, and if you use the Outlook connector, you cannot do this. This post will show how to create an email, how to add an attachment and how to send an email, so that it’s tracked inside of D365/Dataverse.
Create a file or attachment that you want to add onto an email, in my example, I'm going to carry on from my previous post about Word Templates.
Then we will "Add a new row" using the Dataverse Connector and the Table name will be Email Message. This will create an email record/row in D365 which requires certain parameters.
Firstly, the way email fields to, cc, bcc and from work in D365/Dataverse is via what is called an activity party list. This is a list of rows/GUIDs of rows inside of D365/Dataverse that the email message is connected to. This is similar to a lookup, so when you look at the record in D365/Dataverse, you will be able to click on the link and be taken to that row.
You can use the fields “Activity Party Attribute” and “Activity Party Value” in here to build up the array you need for the party list, the first drop down “Activity Party Attribute” will ask if the person will be to, from, cc or bcc, you can choose one of these.
Then for the value, we need to full entity set name and value so contacts(f9340ebb-b1f1-e911-a812-000d3a49e7e9) and put it into this field. This will work for any email enabled tables, so contacts, system users and any custom tables that have been enabled for email and contain an email field.
If you switch to the array input you can see how the array is created/formed.
[
{
"participationtypemask": 2,
"partyid@odata.bind": "contacts(f9340ebb-b1f1-e911-a812-000d3a49e7e9)"
}
]
The ability to use an array here means that you can build a party list dynamically and create as many people in the to, cc or bcc that you like with a variable and loops. We’ll cover that in a future blog post.
You’ll notice the code above has what’s called the participationtypemask and the number defines where the email address for that party goes in the email, from, to, bcc etc.
Next we need to create the attachment for record/row that we will attach to the email. Select the Dataverse connector and the Add a new row action, for Table Name choose “Attachments”. Now we need to do a few unusual things to create this record, the first being we need to enter a formula for the Attachment (Attachments) field. This is because it’s the unique identifier for the attachment row, but also a required field. Usually Dataverse will handle the creation of the GUIDs, but you can override them, this is especially useful when doing migrations.
In this field just enter the function Null and as long as that function is in the field, Power Automate will allow you to save, and Dataverse will take care of creating the GUID. Then we need to specify the entity. I tried both of the options in this dropdown, but neither worked. The name of the entity we want to create the attachment associated with is Email Messages, so looking at the Metadata Browser, the entity name is lowercase email, so we put in a custom value for this field of email.
We need to set the lookup from the attachment to the email record we created earlier, again back to the Metadata Browser to find the EntitySetName which is emails, so we can put that in for the Item (Email Messages) field with the ID of the email we created in the Create an Email step.
For the Body of the attachment, we need to use the Base64 function to convert the file to something Dataverse can understand. We can use the output from the Populate a Microsoft Word Template step from my old flow, we don’t need to create a file in OneDrive for Business or SharePoint, we can just directly use the output from the Word Connector:
base64(outputs('Populate_a_Microsoft_Word_template')?['body'])
The other fields we need to fill in are File Name, which we can specify to be anything we wish as long as it’s got the right extension, and Mime Type but we get this from the Create Email step.
Now that we’ve created the Word template, created the email, created the attachment, the only thing left to do is send the email. For that, Dataverse has a “bound action” called “SendEmail” that will...send emails. Choose Perform a bound action from the Dataverse connector, table name is Email Messages, Action is SendEmail, Row ID is the email we created a couple of steps ago.
And that’s it, how to create attachments on emails via the Dataverse connector.
Ciao for now!
Hello Maybe you had a such experience I Have a request to attach a file from CRM File column to Email creating a new Attachment record is there a Way to create attachment record adding content from CRM file? How could I get CRM file content ? Thank you