Use of Expression in Microsoft flow through an example
Most of the basic flows can be configured using the templates provided by Microsoft. But in real time we will be developing flows which have complexity. Expression play an important role in fulfilling the requirements.
Scenario: - We have project details list which has the below columns, we need to intimate the project team before 2 days of the end date. (Please note that this scenario is picked up from one of the Microsoft insight demo).
The following points needs to be implemented
- Only title and End time are mandatory fields, Start time if not entered needs to be taken as the current date.
- Number of days is the difference between the start time and end time.
- Email needs to be sent before 2 days of the end date.
- If any attachments they need to be attached to the mail being send from the flow.
Below is the complete flow.
Let me explain each step of the flow.
The First one is a trigger, the flow starts whenever a new item is created. The second action is update the item, in the update item we have two expressions
StartTime:- coalesce(triggerBody()?['Start_x0020_Time'],utcNow())
Coalesce function will return the first non null object. If Start time is provided we take that value or we use the current date time.
Num of Days:- div(sub(ticks(triggerBody()?['End_x0020_Time']),ticks(coalesce(triggerBody()?['Start_x0020_Time'],utcNow()))),864000000000)
For calculating the number of days we need the difference between the start time and end time, before doing a subtraction we convert the date time string to ticks.
24 hrs = 864000000000 ticks
We subtract the ticks value for both the start and end times and divide the value by the no of ticks in 24hrs.
We delay the flow till the project is due by 2 days. Here we use the function subtract from time.
subtractFromTime(body('Update_item')['End_x0020_Time'],2,'day')
First parameter will be the End Time string , second parameter is number of a specified time unit to be subtracted (2) and third parameter is the unit of time specified to subtract(‘day’).
Select action is from the data operations connector, it will transform an array from one form to another using expression given in the map. Here we will split the value in email aliases column using the comma seprator. Then we will append @outlook.com to each item of the array.
split(body('Update_item')?['Email_x0020_Aliases'],',')
concat(item(),'@outlook.com')
Here we are getting the list of attachments for the item.
We iterate through each attachment and get the attachment content, then we use the compose action to create a JSON object of the attachments. The json object contains name and content bytes.
Finally we send the email, Attachments field contains the output of compose action.
join(body('Select'),';')
Email aliases are joined using a semicolon and passed into the CC field of send mail.
if(greaterOrEquals(body('Update_item')?['Start_x0020_Time'],body('Update_item')['End_x0020_Time']),'Warning: your start time is greater that end time','')
In the body of the mail we provide a condition if the start time is greater than end time then we display a Warning message.
That’s the end of the flow.