Simple MS Flow to process emails with attachments
Just before weekends I received a new project request for process automation.
The task was as following:
- once new email with attachment(s) comes to specific mailbox the process must store attachment(s) to the specific folder in the SPO library. Within the document library Client has got two layer folder structure. The format of email Subject is "CODE_region_nest_well_<well number>_XXXXXXXXX_DDMMYY", so the name of level 1 folder must be "CODE_region_nest_well" and the nae of level 2 folder (a subfolder) must be "<well number>_XXXXXXXXX_DDMMYY". In case folders does not exist, the missing folder must be created.
So having all that as an input I have decided that to use MS Flow process will be the simplest and best option. Thus I accepted the project and developed the Flow in less than two hours - I took this as weekend exercise for my brains. Although, the process is simple and straight-forward, there were some issues I had to be careful. That is why I want to share this with others.
I set my flow to start automatically and the trigger (as well as first action) would be "When a new email arrives":
I filtered emails with attachment only by setting "Only with Attachments" to "YES" and included attachments, thus I could use them further. Next i parsed email Subject and stored elements to the array:
Once I got needed pieces for folder namings, I initialized two variables to have folder names:
For the level 1 folder I set value:
substring(triggerBody()?['subject'],0,add(indexOf(triggerBody()?['subject'],variables('SubjectParseStrings')?[4]),-1))
Be aware, it is not possible just to deduct 1 from indexOf value. You have to use Add function.
And level 2 folder name:
substring(triggerBody()?['subject'],indexOf(triggerBody()?['subject'],variables('SubjectParseStrings')?[4]),sub(length(triggerBody()?['subject']),indexOf(triggerBody()?['subject'],variables('SubjectParseStrings')?[4])))
I needed to get result of (Subject.length - indexOf), for this I also utilized function Sub.
Set up siteURL - it is possible to hardcode it and get rid of one step. But it is better to have it as parameter - so in case you need to transfer this flow to the other environment you will just adjust parameters.
Next I created two booleans to check whether folders exist or not:
And now I came to the point when I had to query for folder level1. So I simply used "List Folder" action from SharePoint actions bundle:
And now one of the tricky point. I put in parallel two actions - setting boolean values:
The logic is - if folder exists the result of "List Folder" action will be succesfull, if not - we get error. So I just configure "Run After" options to simulate some kind of "try ... catch .." scenario:
Added condition:
And here is another issue. If we leave this as is - we are gonna face with error. So we have to configure "Run After" option for this action as well:
For “Yes” branch added "Create Folder" action:
For “No” branch added actions:
For verifying presence of folder level 2 I used same approach as for folder level1 (don't forget to configure "Run After" conditions).
My final actions was "Create File":
Hope this would be useful for someone in future, as for me - once again I have persuaded Microsoft has got magnificent tools for process automation.
P.S. Here is the solid process: