Power Automate Flow – creating an email with attachment in CDS

By | March 10, 2020

  The way I feel about Power Automate Flow is a perfect example of the love-hate relationships. Yes, I can do much more than I used to be able to do in the classic workflows. However, every now and then I run into something that seems simple, but that ends up being an exercise in google searching, forums browsing, intuition-based Flow development, and, of course, in the intense self-control.

  The subject of today’s exercise was the following question posted in the community forums:

https://powerusers.microsoft.com/t5/Building-Flows/How-to-use-flow-to-send-CRM-email-with-attachments/m-p/494318#M61306

  Well, this seems simple, and it is sort of simple once it’s been done, but it took me a while to figure out how to do it.

  Here is how the Flow looks like:

image

There are a few things to consider:

  • I can create an email record using “Common Data Service (Current Environment)” connector
  • Once an email record has been created, I need an HTTP (Azure Ad) connector to add an attachment
  • And, finally, I need an HTTP (Azure Ad) connector to send the email created earlier

Why do I need those Http with Azure Ad connectors?

image

This is because I could not figure out how to add email attachments/send emails directly through the CDS connector (and there were a few helpful links in the community forums which came up when I was looking for a solution. For example, this one has really helped: https://community.dynamics.com/365/f/dynamics-365-general-forum/360013/creating-activitymimeattachment-email-attachment-record-through-microsoft-flow).

With those connectors, however, you have to use a connection which is using correct base url. For example, in my case here is how “Send the email” step looks like – notice the request url:

image

In order for this to work, you’d have to create a connection so that base resource url and azure ad resource url would match the request url above:

image

image

Also, do keep in mind that all references are supposed to be in the following form:

/entityset(guid)

image

With all that said, let’s just see how each step in my workflow is implemented:

1. Loading the file from Sharepoint

image

2. Creating an email (using “create record” action of the “Common Data Service(Current Environment)” connector)

I’ve hardcoded contact guid there, but you might, as well, get it from CDS using “List Record” action, for example

image

3. Converting email message id into “any” type – that’s a Compose action

image

This is so I could use it in the next step

4. Creating an email attachment (Http with Azure AD)

“Outputs” is from Get Email ID above

That base64 function is an expression: base64(outputs(‘Get_file_content’)?[‘body’])

I’ve also hardcoded file name & mimetype

image

5. Finally, sending an email (Http with Azure AD)

image

That’s it. Done. I think in the end I love Flows! Since, of course, I would not even be able to load that file from Sharepoint using a classic workflow, so I’d have to write some code, and it would take me quite a bit longer to figure out Sharepoint authentication etc.

2 thoughts on “Power Automate Flow – creating an email with attachment in CDS

  1. Arjun

    Brilliant Post! hats off you to you Alex. Keep it going as usual.

    Reply
  2. Jim

    Alex,
    Can I send multiple emails with different attachments? To be more specific:

    Email A–> Attachment A
    Email B–> Attachment B
    Email C–> Attachment C

    It would be even better if I can send multiple emails with a various number of attachments:
    Email A–> Attachment A1, A2, A3
    Email B–> Attachment B1
    Email C–> Attachment C1, C2, C3, C4

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *