Using Logic App to send multiple attachments from Azure Storage Account

(2021-Apr-23) Memory is an interesting thing, you may not remember all events from your past, but some sparking moments keep extending their bright beams of light. With the growing time gap between your present days and those moments, their visual recollection seems as real as today’s sunlight passing through a window.

I remember during my first year of technical school, we had a computer programming class. There was one 2nd-year student who knew Pascal programming language very well and he was OK from time to time to help me with some of the new coding skills that I was trying to develop. After coming to my desktop computer for the 2nd or 3rd time one day, while I was debugging my code with many errors, he finally said, “Learn, how to solve these problems yourself”.  This was one of the best software development advice that I had received in my life: to own a problem and try to resolve it myself, help was still available if needed, but I had a responsibility to work on it first. Fun fact, that software development is not the only area where this principle could also be applied :-) 

I’m thinking that in terms of learning a new technology, a person could go through 3 stages of personal experience: (1) following step-by-step tutorials that explain basic and advanced examples for this technology - “guided learning”; (2) learning what is possible for this technology and then creating your own use-cases with it - “learning through exploring the potential”, and then (3) the ultimate and “unexpected learning” experience where new and unexpected use-cases for this technology can be discovered by pure accident. Similarly, Stewart Adams, inventor of Ibuprofen painkiller, discovered that this medication can also be used for hangover treatment.

OK, enough for the starting part of this blog post, let’s get to the point. I’ve passed my “guided learning” experience of the Microsoft Power App development by following available examples (still I consider myself as a newbie there), and I definitely haven’t reached the “unexpected learning” stage yet. 

In my real project, I need to build a Logic App to send email messages with a set of files attached from my Azure Storage Account. I was able to find similar examples from other power platform developers, however, they lacked a critical part that I needed: my set of files had to be dynamic: 2 files, or 102 files -  the Logic App should be able to support this.

So, here, I would like to share my brief journey in creating such Azure Logic App:

Brief solution concept

My main goal is to use Logic and pass the following data elements: text values for [(1)Subject,  (2)To, (3)Message Body) fields and JSON formatted array with the list of (4.a) file names and their (4.b) Azure storage account locations. This will result in a proper email message and required files attached.

Solution details

1) Text variables setup

There are three payload components that I want to save into variables right away (Subject, To, Message Body) for easy access during the logic app workflow development


2) Conversion JSON text list of files into a real JSON object 

Since the HTTP request of my Logic App is a JSON object already, then I can simply pass a JSON list of files as a text field within. This results in an additional step to convert that JSON textual list of files into a real JSON object with the help of the "Compose" data operation:

The Content field of the "Compose" task is configured the following way: @triggerBody()?['FileList'].

The Schema fields to parse my text JSON file is configured this way:

3) Saving the output of the Compose JSON conversion task into a variable

The previous step generated an output result in a pure JSON array format that can be easily iterated through and therefore I will save it into an object Array variable.


4) Building Attachment Set

Having a real JSON array in hand, now it's time to iterate through this list of file names and their locations and build one object with multiples physical files within.

The "For Each" loop container is used to take the "var_file_list" variable value as input and then it:
(1) Gets the content of each file using the "Path" JSON elements
(2) Incrementally builds "var_attachements" object by adding content of the extracted file, by using these two attributes:
  ContentBytes: @base64(body('GetBlobContent'))
  Name: @items('Build_Attachment_Set')?['DisplayName']


5) Create an HTML table with the list of files

Then I create an HTML table object with the list of files that I plan to communicate in my email message (this step is optional).

6) Email message composition

Then I compose an Email message that includes all necessary elements from the workflow that I've just created:
- Subject
- To
- Message body, which includes both Logic App HTTP request message and HTML table from the previous step.
- Attachments that contain multiple files from my Azure Storage Account.


Summary

My mission is accomplished and now I can reuse this Logic App to send email messages with multiple attached files based on the requested list. This workflow works with 2 files, it works with 20 files and it should work with many more files if they're not too big.


Going back to the beginning of my blog post, I want to give credit to a very good colleague of mine Kenwyn Warner, who shared his knowledge and helped me to understand some of the key concepts of the Microsoft Power App (Logic App) development environment. I believe this could be a nice example of the 2nd stage of my learning experience :-)

Comments

  1. Thanks for sharing this article ...

    ReplyDelete
  2. Hi Rayis,
    This is great, I have similar requirements.
    Can you please post source code to GitHub?
    Thanks

    ReplyDelete

Post a Comment