Introduction
In this post, we look at how to use Power Automate to create and send emails with attachments in Dynamics 365/Dataverse. When working with Dynamics 365 or Dataverse, one of the key advantages is tracking emails, and if you use another email connector like Outlook, you cannot do this. This post will show how to create an email, add an attachment and send an email so that it’s tracked inside D365/Dataverse.
Table of Content
- How to Create and Send Emails with Attachments Using Power Automate
- How it Looks When Sending Email With Dataverse
- Conclusion
Prerequisites
- Familiar with Dataverse
- Familiar with Dynamics 365
- Familiar with Power Automate
- An environment and a queue/system user that is configured for sending emails
How to Create and Send Emails with Attachments Using Power Automate
In this example, we use the essence of the flow from the Power App Award Generator blog post, but instead of sending the email with the Outlook connector, we’ll use Dataverse. This allows email tracking in Dataverse/Dynamics. The flow looks like and we’ll go through each of the steps below the image.
Step 1: The trigger used in this example is the Manually trigger a flow with the same parameters as in the blog post, Power App Award Generator. The values in these parameters is used to populate the Word Template later in the post.
Step 2: In this step, we’ll create a compose action that creates the naming convention for the documents generated.
The expression in the compose action outputs the following 2022.12.10_Chef_of_the_Year_John_Doe. The naming convention is a combination of the following expression.
// Formats the date in the parameter DateOfCompletion to be like this, 2022.12.10 formatDateTime(triggerBody()['text_2'],'yyyy.MM.dd') // Replaces spaces in the parameter TypeOfCertification with underscore, like this Chef_of_the_Year replace(triggerBody()['text_5'],' ','_') // Replaces spaces in the parameter FullName with underscore, like this John_Doe replace(triggerBody()['text_3'],' ','_') // When putting it all together it looks like this 2022.12.10_Chef_of_the_Year_John_Doe formatDateTime(triggerBody()['text_2'],'yyyy.MM.dd')_replace(triggerBody()['text_5'],' ','_')_replace(triggerBody()['text_3'],' ','_')
Step 3: In this step, we’ll create the email activity pointer used to send emails. This is the configuration of who is the sender and receiver of the email. In the image below we can send an email without tracking it to a contact by only referencing the addressused field. Later, we see how we can track the email to a contact and send it from a system user instead of a queue.
Below, we can see the email activity pointer for sending it from a queue and to an email address.
[ { "participationtypemask": 1, "partyid@odata.bind": "queues(d9da8b52-8378-ed11-81ac-000d3ab79606)" }, { "participationtypemask": 2, "addressused": "@{triggerBody()['email']}" } ]
NOTE: In the reference to system users and contact you usually add dynamic values from a for instance a Dataverse trigger or list rows and not hard coded values. The hard-coded values are for testing purposes only.
In the next example, we look at how we can track the email to a contact and send it from a system user instead of a queue.
[ { "participationtypemask": 1, "partyid@odata.bind": "systemusers(d9da8b52-8378-ed11-81ac-000d3ab79606)" }, { "participationtypemask": 2, "partyid@odata.bind": "contacts(f5973462-768e-eb11-b1ac-000d3ae92b46)", "addressused": "@{triggerBody()['email']}" } ]
Step 4: Next step, create a file that we are going to attach to the email. In this case, we are resuing the template from the post mentioned earlier, the difference is that we are using another trigger parameter. To see more in detail how the generation of the document looks like, see the post Power App Award Generator.
Step 5: Furthermore, we need to create an email message that the attachment is related to.
Step 6: Further on, we’ll create the generated document in OneDrive as an attachment in Dataverse, we’ll do this by following the steps below.
Things to keep in mind and also commented on in the images below.
First thing, when looking for the attachment table, select the first in the list.
Second, is when selecting the Entity value. Do the following, select the dropdown and choose Enter custom value > Enter email in lowercase letters otherwise it won’t work, and you’ll get an error regarding object type.
Lastly, we’ll send the email message with the attached attachment.
In the next part, we look at how it looks when the flow runs and how it looks in Dynamics.
How it Looks When Sending Email With Dataverse
This part shows a GIF of how it looks like when the flow is running and the result in Dynamics.
Conclusion
And that’s it, how to create attachments on emails via the Dataverse connector.