Power Automate flow Expressions Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Great content! thanks OP

👍︎︎ 2 👤︎︎ u/sc0rch3df0x 📅︎︎ Oct 19 2021 🗫︎ replies
Captions
hello everyone this is reza durrani today's video is a step-by-step tutorial on power automate expressions we will look at the different functions available in expressions look at how we can use them in actions conditions trigger conditions and a lot more so let's check this out in action [Music] expressions are a set of functions that can be used to perform specific actions within flows string manipulation working with dates and times converting data types performing logical functions and mathematical calculations performing complex conditional statements and a lot more the anatomy of an expression i have a sample expression function called replace wherein we define the original string then the text that we would like to replace and then the new text with which we would like to replace it with here is some sample data with the replace function replace the text expressions are difficult replace the text difficult with the text fun so with this expression the function here is replace and this function accepts three parameters so the basic anatomy of an expression is a function and then it may have one or more parameters you can also nest expressions the output would be expressions are fun the word difficult replaced with the word fun expressions can be used in any action in flow can be used in any condition in flow can also be used in the trigger action of your flow very important to trigger the flow or call your flow only when you truly need it the expression editor as of today comes in two flavors one is the classic editor and the second one is the modern editor currently the classic editor is generally available and the modern editor is the one that is an experimental feature soon to go into general availability in the classic editor the space to enter the expression is very limited in the modern editor the expression space is large so if we were to plug in long complex or nested expressions in the classic editor it is difficult to see the entire expression in one go in the modern editor because of the real estate that is provided for us we can easily see the entire expression at once in the classic editor if you need to get data from dynamic content in a flow you have to switch over to the dynamic content tab and then pick the dynamic content values in the modern expression editor as we are plugging in the expression if we need dynamic values they are provided to us right at the bottom of the expression editor experience itself the classic editor requires the user to click a button to confirm that the expression that they've entered is complete in the modern editor if the user was to just click out of the expression experience the expression would be automatically stored to start with i'm going to use an instant cloud flow that triggers manually i've given this flow a name to learn the basics of expressions i will select a data operation action and power automate called compose this acts just like a static variable in flow now to begin plugging in your expressions when i select specific property of an action we can see how this dialog box appears which gives us dynamic content which is dynamic properties that all the preceding actions in this flow are exposing in this case i just have one action which is my flow being triggered and these are the properties that that specific action is exposing to plug in expressions we need to move over to the expression tab let's take one example i will use a string function called concat which concatenates or combines any number of strings together so i will select this open the bracket now intellisense is guiding me through it says this attribute is required the string to combine so i'm going to type in power in single quotes then i can provide another attribute here so let's say i just provide a space and i can keep adding additional parameters to this specific function so my third parameter here would be automate now that completes my expression it is important that i hit ok so that the expression gets saved let's go ahead and save the flow and let's test this flow now once the flow run is complete if you look at the compose action the output here is power space automate now back to the expression if i select this it will open that expression experience so i can make changes to this gone ahead and added another parameter to this and i have to ensure that i click update if i do not and just click anywhere outside that expression box now if i go back to that expression you will note that my changes were not saved additionally if i've really zoomed in to my browser the expression dialog does not even appear only the dynamic contents are available here for me to choose so ensure that you have a decent browser resolution in order for you to get this dialog box for expressions this is the classic expression editor in which the only real estate we have to play with is this i can write long nested expressions for example i can write another concatenate function on top of this so concatenate the result of this concatenate with additional attributes so let's say i concatenate this with flow there's not enough real estate for me to look at the entire expression together one tip is you can copy your expression i'll click update so it updates it if you head over to three ellipses there's an option for us to write notes here i can plug in that expression so whenever a user looks at the flow they can see the expression this is static text if i was to make a change to the expression the note will not change i have to go back and reset the note if you hover on the expression function it will showcase the entire expression underneath it another key option that we have is something known as peak code any action if i go to three ellipses and head over to peak code you can look at the code behind that action i can see that expression function right here it will always begin with this at symbol in the classic editor we are also provided with categorizations of these expressions like string functions collections logical functions and so and so forth and in each of these categorizations if you go to see more it will list out all the functions also right here at the top there is a link called learn more if you select this it will directly take you to the expressions reference guide this is docs.microsoft.com each and every expression function type is listed right here so let's take an example string functions concat if i select this it will give you an example the types of parameters it expects and the return type that it provides you also have sample expressions here that you can literally just copy go to your expression editor just place it in there click update that has now updated my function work with the modern expression editor because currently it's an experimental feature we have to go to the settings here on the top right go to view all power automate settings and turn on the experimental features bear in mind this is being turned on only for the current logged in user i will go and turn this on and click save and once you do this it will refresh the page basically refresh your flow now in my case since i did not save those changes if you notice when the screen refreshed i lost those changes so make sure you have saved all your changes first before you switch over to the experimental features if i was to add that same compose action this time we don't get that pop-up dialogue experience rather we get this new experience the first one is dynamic content and the second one is expressions and the expression box here is extremely large and to get to the list of all the functions that's the link right here if we select this we are back to the reference guide for expressions now let's take an example of another string function we'll pick replace so i'll just go ahead and copy the sample example go back to flow paste my function right here we do not have any button that we need to click the way you save the expression is literally by just clicking out also if for some reason there is an error in the syntax if i click out it will let me know that there is an error what is the error the expression editor does not specify that in case of the classic editor it won't allow you to even save that expression until you correct it so once i've fixed it if i select out if the expression is correct we can see the expression function show up if i was to run this flow if you look at the output here it's the new string in this flow i've gone ahead and plugged in some of the most commonly used string functions we have a string function called sub string wherein you can define the index from which you would like to begin the substring from and then put in the length of how many characters you want from that string my case here go from zero to five basically pick the word power here is an example of nested expressions related to string functions i'm using concatenate so concatenate random good there is a function called good that randomly generates a good there's a function called two lower that converts the text to lowercase there is one called two upper that converts the text to uppercase one powerful string function is called split split a string based upon the character that we have defined and the output of the split function is an array so let's test this flow out the substring function in action is right here the output is power and here is a random grid that got generated the text power and lowercase the text automate in uppercase there is a function called index off that gives us the index of a specific text that we provide right here so index of the letter t gives me the first index of where it finds this letter the index is two there's a function called the last index of gets the last index of the string that you've specified here in my case the letter which is a is sitting at the index 11. starts with checks to see if the string starts with the text that you have specified and it returns a boolean value of true or false please note this true is different from the text true that you type in there's a function called trim if you would like to trim the white spaces and here is the output of the expression split gives me the data in the form of an array or a collection and the key to node for arrays or collections is you will see these square brackets let's look at some logical and math functions i have a variable of type string i'm using the contains function to check if the variable contains the text reza now to write an expression like this i'll start typing the function name intellisense will guide us through so contains i need to provide the string or the array as the first attribute and that i'll prick from dynamic content i'll pick my variable called name so it'll just plug in the expression right there and then comma search for the text browser we have equals that checks to see if the string equals the value that we've provided logical expressions of ands and r's that we can combine does the variable name start with the letter r and does the variable name end with the letter i we want to use this same expression and a condition i'll head over to condition we'll go to expression and plug that expression in here and click ok output of this expression is true or false equal to here it is very important for us to go to expression and type in true or false this is an actual boolean object of true let's test this out in action does the variable name contain reza it's true does it equal raza dorani that's true does the variable start with r and end with d it's true and for our condition it evaluated to true so it went to the yes branch get me the minimum value from an array of numbers the minimum value here is one we also have a max function so that gives me the number nine generate a random number between one and five hundred another function is called range create a range from zero to ten basically give me an array of numbers here that has ten values in it so it goes from 0 to 9. when working with dates and times with power automate very important to note flow works with dates and times in utc format only if i need the current date and time there is a function called utc now we have a function called format datetime and you can specify the format in which you would like the output to be provided in if you would like to convert it from utc to any time zone of your choice we have a function for that as well it's called convert from utc give it the date and time which time zone do you want to convert it into and then which format do you want to use for the output we also have functions to add these subtract days add 3 days to the current date and time convert that to the central standard time and then output it in the following format if you would like to subtract three days from today the function is the same ad days but we just append a negative integer if i need the day of the week the function is day of the week utc now here are the outputs utc now gives us the current date and time in utc format this is with the format being applied this is the current date and time in my time zone i'm recording this video as of the 14th of october three days from today is the 17th three days before today is the 11th and it's a thursday which is the fourth day of the week there are also collection functions that we can leverage here i have two simple arrays of cars there's a function called contain where you can check if the array contains a specific value we can check the length of an array we can check to see if the array is empty we can get the first value get the last value skip specific number of values in the array this will also return an array get an intersection between two arrays return the common values between the two arrays get a union of the values from both the arrays here are the outputs does the array1 contain bmw it's true what's the length of the first array it's 3 is the array empty the answer is false what's the first value of array1 it's ford what's the last value was fiat skip the first item in my first array so it will return the remaining values of the array which is bmw and fiat get me an intersection of both my arrays the only common factor was bmw and then get me the union of both my arrays here are the results when we add actions and flow to get data from our data sources or data services or maybe call apis using the http connectors the response is more or less going to come back in json format it's very important to understand how we can parse the output of json i have a simple json array of data in this i have multiple objects that have properties and values associated with them now one common way of parsing the data here is by using the pass json action using the output of sample json and for the schema you can literally just copy the data that you have in your json object go to generate from sample click done it will automatically provide the schema and here parse json will provide all those properties related to the sample data that we've provided now this works well in case of simple json structures however in case of complex json structures parse json might not be your best friend reasons are they could be properties that are similar so for example i have id here i have id here as well parse json will give me id but which id am i utilizing many times it won't pick the schema of the underlying objects because you need to parse json and then do another parse json if you know how to traverse a json object by using expressions we kind of avoid all of those challenges so let's take this example i'm going to copy this i'm going to paste this in my sample json data now let's say i need the batter add a information for the first item in my array now to get that without using any parse json action go to the expression editor pick the output of sample json i need the first object so there are two ways to get it one is by using the first function and second is by directly specifying the array index the index is 0 which is the first item and in here i have a property called batters in there there's another property called batter and that also returns an array my next property would be batters again in square brackets and the single quotes the name of that property and then the name of my next property to get to that final part now if i was to click outside that saves the expression and if i run the flow if we look at the output this is the array of batter information for that first item now in this as well if i just need the first value all i have to do is specify the index now if you were to run this if you look at the output gives me just that first value now when you write these expression functions if we were to point to an attribute that does not exist for example get me the index of the item at position 100 or maybe i have spelled batters incorrectly now in this case if i was to run my flow my flow will fail that's because when it's trying to traverse through the array it's not able to find a specific index or a specific property that you've specified so if you want to ensure that you're performing null checks as well all you have to do is use the question mark symbol check to see if there is an array at this index if it is only then proceed ahead in this otherwise the response is going to be null and here i'm going to keep putting question mark after every aspect of that json object that i'm passing through observe how i do not get an error and the output here says click to download if i select is just empty basically the output here is null and if i want to confirm if this is null i can use the function called equals is this equal to remember null is also an expression object just use null now if i was to run this flow we can see the output here is true because that is null another way you can write this notation for indexes you have to give the array index but when you have multiple properties like i have right here i can even replace this with batters slash batter so it knows that it has to traverse through that specific set of attributes so let's check this out i'll put this as index one that's the second item go and get me the first property of the batter object and this is the output this comes in handy in a lot of scenarios one of them is to avoid loops i have a flow that triggers when a new item is created in a sharepoint list called travel requests the moment the flow triggers i grab the created by users manager from ad then we update the status of the item to pending and we use the approval action and flow to begin the approval process now the approval action has a dynamic property that it provides called outcome when we can check the outcome of the approval so is it approved if yes i update the status and then i send an email to the original creator that the travel request has been approved if it's rejected we send rejected information here when i'm sending the approval email if i want to plug in the commands of the approver if i pick responses commands observe how it adds this applied to each loop the reason is because responses is a property of type array and whenever flow comes across an array property type it will add this loop in my scenario i know that the approver is always going to be a single user so i just created an item which has triggered this flow once the approver approves the request if you look at the flow in action the action start and wait for an approval and specifically focus on the outputs the output here is in the form of json if i was to plug this in visual studio code the body attribute has another attribute called responses and this is an array and in this array we have one responder and the commands that the responder button was okay now the reason why we get that loop is because of this array now if you want to avoid this loop you can do that by leveraging expressions i go to functions if i search for body the start and wait for an approval action has that value so i'm just going to pick it now this is the attribute body now within this i need responses which is the array so i write the following function to traverse through that json i know that in this process i only have one approval so i'm just going to plug in array at index 0 and from this i need the commands i'm just going to plug in commands we've avoided the for loop action completely by traversing through the array of json data this process starts for every travel request that gets created maybe my requirement is we need to start the approval process only if the estimated airfare is greater than 100 so in order to do that i have to start my flow conditionally if you go to three ellipses here and go to settings this is where you can plug in something known as trigger conditions wherein you can conditionally trigger your flows and you can add multiple conditions and they all run together with the logical condition and i don't get the expression editor here so how do i plug in that expression here's a tip add a compose action right after this and grab your expression from here let's go to the expression syntax so i have estimated airfare i pick this it plugs in the function to grab that value i will use the greater than or equals function is this greater than or equal to 100. so i'm just going to copy this go to my trigger go to settings go to add and paste it right here important to note you have to put the at symbol at the beginning of the trigger condition here i'll click done the compose action is no longer needed just go ahead and delete this i'll save the flow and now the flow is ready to run only when travel requests are greater than 100 and let's test that out i've created a couple of travel requests one with the estimated airfare s50 the other one is 500. if you look at my flow runs there's only one floor running and this one would be for the travel request that has the value greater than 100 which is the trip b and to confirm that if you look at the title right here it states sample trip b now this is extremely handy because i can trigger my flow based on my specific use case and what i do here is i save flow runs i save something known as api calls every action that you add in your flow every step get manager update item start approval condition if i have a loop that runs 10 times and let's say that loop has five actions within it those are 50 actions all of these actions are counted together and they are known as api accounts or entitlements if you look at the documentation on these api requests or limits the number of api requests a user can make in a 24 hour period is associated with the user's license for a standard office license user you only have 2000 api requests per day you can save api requests by leveraging trigger conditions by leveraging expressions by avoiding loops beauty of expressions is you can nest expressions many a times i see folks writing multiple conditions first condition to check a scenario then another condition to check another scenario and so on and so forth each condition is also counted as an api request so why not just write one long condition right here by writing an expression if your flow runs into any issue due to a runtime error which causes the flow to fail the flow history will just map it as the status field and as the owner of the flow you will receive an email that says that your flow has failed what if i want the flow to notify me of the flow failing i also need the link directly to that flow run i have a get items action against my sharepoint list and in here i'm trying to fire a filter query against my sharepoint list of expense reports and in this list i do not have any column called column a and i'm trying to query this column basically i'm trying to generate an error here on purpose now if this step was to fail i need to send an email to my admin or my owner of the flow here i've added a parallel branch in my flow and for this action i'm going to configure run after and stated that run this action if the preceding action which is get items has either failed or skipped or timed out for some reason if this fails then i need to go ahead and send an email to the owner of the flow and give them a link to the flow run now how did i get this floor on link once again through expressions there is a function called workflow basically what i'm trying to create here is a direct link to the flow run and to do that you would need to get your standard url which i've grabbed right here in my case i'm in the united states region so that's my url up until environments concatenate this with i needed the name of my environment and which the flow is running so that i can grab directly from this expression then flows then i need the name of my flow the unique id of the flow i can grab it directly from workflow name and then i need the run link to my flow and that is also available in that workflow object let's test this out this flow has run it has failed but i've handled that and i've sent a notification out as the admin i get this notification email that my flow has failed and i have a direct link if i click this it will directly open up that exact flow run for me i can even see the details of the error right here on the right hand side i have a service desk application in which tasks are assigned to users and there's a due date associated with each task i would like to send an overdue task reminder to all the users whose tasks are overdue my flow runs on a schedule which is daily at 8 am grab the data from my list which is called service desk and right here i'm firing a filter query against a column called due date where the due date is less than or equal to and here's an expression utc now get me the current date and time is the due date less than the current date and time if yes those are the items that are overdue james has three tasks that are overdue i would like to just send one email to james here i'm using the union function and i'm comparing the same array with itself then i loop through that array i grab all the information calculate the overdue days if i look at the scenario where the tasks are assigned to me i only have one task that's overdue and the email that i have received gives me exactly that information that's the task that's the due date it's been overdue since 48 days i have a document library in sharepoint i have folders defined based on contracts and each of these folders have a unique property called contract id and my use cases when my external users or contractors send me documents in an email they also share the contract id in the mail of audi now when i receive that email i want to automate the process of picking those documents and putting it into these specific folders here i have a user who's going to send an email which has the contract data and in the body they're gonna specify the contract id now once they click send and once that email arrives in my mailbox this flow is listening and once that flow gets triggered it goes and grabs the contract id from the body of that email using expressions then goes and gets the context of that specific folder starts creating the files that were attached in the email in that specific folder if i head over to that specific folder we can see those files coming in right here and here is what the expression looks like i needed to check the data in the body of the email trigger body body preview and that's easy to grab directly from dynamic value i needed to split it by the return and the new line character because if we look at the body of the email the data will always come in this specific format and to do that i use another function called decode uri component and this equates to those characters now split returns an array and i want just the first value of the array because i just need the first line that's where the contract id is in i once again split it with the text contract id so when i split this i will again get an array and in this array i need its second value which has the contract id i use the index one fire a query against my library where the contract id matches the id that i created right here and once i get that i loop through all the attachments of my email and start creating the items if you observe here for the create file action when i'm defining the folder path i have used an expression and the reason why i've done that is to avoid a for loop because the get files action returns an array of data from the body of get file properties go to the value property which is an array grab the first item of the array and in there go and grab the full path calculating the number of working days based on a given date range when i run this flow it will ask me for the start date and the end date calculate the number of working days by excluding the weekends saturday sunday and remove the holidays based on my company's holiday calendar let's say i pick november 1st to november 30th and run this flow once the floor uncompletes i get this email the total number of working days between the dates that i entered in my case november is 22. there are two upcoming holidays for thanksgiving based on my company's holiday calendar what are the different expressions the flow is using calculate the difference in days between two dates i have a blog on this the link will be in the description for this video next i'm using the range function to create an array based on the days difference of the dates and then for this array add days to the start date that the user has specified and i'm using this property called item whenever you're running loops if you want the context of the current running item just use the item function then i grab my company's calendar for holidays then i go ahead and filter or remove all those dates that match my company's holiday calendar then i remove the sundays and the saturdays and here i'm using that day of week function that i showcased earlier six is saturday zero is sunday and then finally i'm sending out that email and here i'm using that length function to calculate the length of the array which is the number of days power automated reads and processes dates and times in utc format if there is a scenario in which you would like to notify the user of a specific date it's important that you convert that into the user's time zone rename actions with meaningful names throughout your flow because more or less when you're using expressions you will be pointing to the names of your actions the expressions when it gets too long you can add it as notes to your actions and conditions which makes it easy to read the underlying expression remember p code is your friend you can go to the actions and go to p code and grab the expression for json objects use the question mark property for reading optional properties the array notation in json objects is the square brackets arrays begin from index 0. and when updating your expression specifically in the classic editor make sure you hit that ok or update button because if you do not your expression will not save if you enjoyed this video then do like comment and subscribe to my youtube channel and thank you so much for watching
Info
Channel: Reza Dorrani
Views: 11,546
Rating: undefined out of 5
Keywords: power automate expressions list, microsoft flow expressions, flow expression replace, flow expression split, power automate tutorial, power automate, tutorial, reza dorrani, power automate expressions, learning expressions, introduction to expressions in power automate, logic apps expressions, power automate examples, power automate json, flow expression if null, power automate expressions reference, flow expression with dynamic content, flow expressions, flow expression
Id: O97IdCbfMQI
Channel Id: undefined
Length: 35min 39sec (2139 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.