cancel
Showing results for 
Search instead for 
Did you mean: 
LinnZawWin

Send Email from Dynamics 365/CDS with Attachment from Notes Using Flow

Automation of email sending out from the system is a common requirement in Dynamics 365 applications or model-driven apps for Common Data Service (CDS). The requirements of automated emails can be an approval email to the case owner, reminder email to the task assignee, status update email to the customer, etc. In some scenarios, automated email requires to include attachments from the Notes (annotation) related to the record or SharePoint files from the document folder of the record.

Send%2BEmail%2Bwith%2BEntity%2BAttachment%2Bfrom%2BNotes%2BHeader

Before I built my flow, I did a quick web search for the existing solutions. I found a few great tutorials on how to create an email with attachment in CDS using Azure Function or HTTP with Azure AD. Those blog posts inspired me to build my version of it and find out if it can be done with just CDS connector.

Problem: There are two Attachment entities in Dynamics 365 and CDS connector shows x2 "Attachments" in the entity selection list. When the entity name is chosen from the list of Entities in the CDS connector, the first one is ActivityMimeAttachment and the second one is Attachment. Those who are familiar with creating email attachments in Dynamics 365 using SDK know that it needs to be created using ActivityMimeAttachment entity. But the problem with creating ActivityMimeAttachment is that the lookup field to Attachment entity is mandatory and the system would not let to save the record without that value. Then, I attempted to create an Attachment entity and populate that lookup but the flow failed with the following error.
The 'Create' method does not support entities of type 'attachment'.
To resolve those issues, I had to come up with a workaround solution as mentioned in steps 5 and 6 using both CDS connector and CDS (current environment) connector.
Attachment%2BEntities

 

In this blog post, you will learn how to achieve the following using the automated flow from Power Automate.

  • retrieve the files attached to the entity in Notes (annotation)
  • retrieve the queue to set as an email sender
  • add attachments to the email entity
  • send email from Dynamics 365/CDS using bound action

The Use Case: Case office generates a successful letter and attaches as a note to the case entity. Then, the case status is updated to Approved and the Application Successful email is automatically sent out to the customer together with the successful letter attachment.

Sample Flow: The following sample flow is the simplified version of to above real-life scenario which is built with just out-of-the-box CDS entities (without Case entity). The sample flow triggers when the Contact record is assigned and it emails to the new owner of the record with the note attachments of the record.
This is the summary of the solution and the detailed explanation of the flow steps can be found below.

  1. Trigger the flow
  2. Retrieve Notes with attachment related to the entity
  3. Get the sender mailbox queue
  4. Create a draft Email record
  5. Create an empty Attachment record
  6. Update the Attachment record from the step above with file content from the Note attachment
  7. Send out the Email from the system

Send%2BEmail%2Bwith%2BEntity%2BAttachment%2Bfrom%2BNotes

 

1. Trigger the flow
1.%2BTrigger%2Bthe%2Bflow

The flow triggers when the Contact record is assigned (the owner field is updated)

 

2. Retrieve Notes with attachment related to the entity
2.%2BRetrieve%2BNotes%2Bwith%2Battachment%2Brelated%2Bto%2Bthe%2Bentity

 This step retrieves the Note records of the Contact entity with attachments (isdocument eq true)

Only 3 fields are required to be used in the later steps. (filename,mimetype,documentbody)

 

3. Get the sender mailbox queue
3.%2BGet%2Bthe%2Bsender%2Bmailbox%2Bqueue

This step is just to retrieve the queue to set as the email sender From field. (the mailbox from which the email is to be sent out)

 

4. Create a draft Email record
4.%2BCreate%2Ba%2Bdraft%2BEmail%2Brecord

In this step, a draft Email record is created to add attachments.

  • The Queue record from step 3 as a sender in From Activity Party. Assuming there is only one queue with that mailbox name is specified, the first() function is used to get the only record in the entity collection.

(first(outputs('List_Mailbox_Queue')?['body/value'])?['queueid'])

  • The new owner User as a recipient in To Activity Party.
  • The triggering Contact record in the Regarding field.
  • And some value in Subject and Description (email body)

5. Create an empty Attachment record

 

🛈 Important 
This step needs to be created using the standard Common Data Service connector to set NULL value to the lookup field. NOT Common Data Service (current environment) connector.

 

5.%2BCreate%2Ban%2Bempty%2BAttachment%2Brecord

 Once the draft Email is created, the next step is to loop through the Note attachments from step 2 and create an Attachment (ActivityMimeAttachment) record.

As I mentioned in the problem statement at the beginning of the post, the Attachment field is mandatory for Attachment (ActivityMimeAttachment) record. Since the CDS (current environment) connector cannot be used to set the null value to the lookup field, the Create action of Common Data Service connector needs to be used.
Select the first one out of x2 "Attachments" in the Entity Name list for Attachment (ActivityMimeAttachment) entity.
Set "null" value using Expression for Attachment field.
Enter custom value "email" (not integer Object Type Code) in Entity Value field, select "emails" in Item Type and the Email Message unique identifier from the Email record created in step 4.
The problem with Attachment (ActivityMimeAttachment) entity Common Data Service connector is not being able to set the actual attachment file information (such as file name, content of the attachment, Mime type). That is why there is another step to update with the file information.

 

6. Update the Attachment record from the step above with file content from the Note attachment
6.%2BUpdate%2Bthe%2BAttachment%2Brecord%2Bfrom%2Bthe%2Bstep%2Babove%2Bwith%2Bfile%2Bcontent%2Bfrom%2Bthe%2BNote%2Battachment

 This step is to set the actual attachment file information to the Attachment (ActivityMimeAttachment) record created in step 5.

For this Update step, Common Data Service (current environment) connector needs to be used.
With the Common Data Service (current environment) connector, Update action of Attachment (ActivityMimeAttachment) will show additional file information fields and populate Body, File Name and Mime Type fields need to be populated from the retrieved Note entity as in the screenshot above.

 

7. Send out the Email from the system
7.%2BSend%2Bout%2Bthe%2BEmail%2Bfrom%2Bthe%2BsystemOnce all of the attachments are created, the last step is to send out the draft Email using Perform a bound action step from Common Data Service (current environment) connector.
When the Email Messages is selected for Entity name, "SendEmail" Action will be available for Action Name.
Populate the Email Message unique identifier from the Email record created in step 4 into Item ID and IssueSend needs to be set as Yes. (then, the system will update the status of the Email record to "Pending Send" and the server-side synchronisation will pick up the email.
If the IssueSend is set as No, the email status will be updated to "Sent" and the email will be just created as a sent email. (but not actually delivered out of the mailbox)

 

Summary
By using a combination of the Common Data Service connector and Common Data Service (current environment) connector, we can create the Attachment (ActivityMimeAttachment) for the Email record in Dynamics 365/CDS.

You can download the above sample flow from my GitHub repository via this link or the attachment in this post.

This blog post was originally posted at http://linnzawwin.blogspot.com/2020/05/send-email-from-dynamics-365cds-with.html

About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/