Hello Friends,
Welcome back with another post on Power Automate. As we know, while creating a flow, we all use variables of Integer type, String type, Boolean type and sometimes Array type. Apart from that, did we observe one more type of variable "Object" type. I guess, we would haven't observed it. This is an amazing type of variable. If you are using lots of variables in your flow, then surely it will help you in replacing all those variables with a single one. As the type of this variable is object, we can store any object in this type of variable. The simplest way is to store a JSON in this variable. As we know, JSON contains the Key-Value pairs, thus it is the simplest and strongest part of JSON. The combination of JSON with Object type variable is an amazing combination. Let's see.
- Open the Power Automate Portal. Click on "+ Create", select "Instant cloud flow".
- Give your flow a suitable name. and select the appropriate trigger condition. For demo purpose, I am selecting "Manually trigger a flow".
- Click on "Create" to create the flow. Now, we will add 2 variables
- arrRandomNumbers (variable type - Array)
- objCounts (variable type - Object)
- The objective is that we will generate a series of random numbers between 0 & 9 and count how many 0s, 1s --- 9s are generated. For each number, we will add a key-value pair (or a property in object variable).
{ "CountFor0" : 0, "CountFor1" : 0, "CountFor2" : 0, "CountFor3" : 0, "CountFor4" : 0, "CountFor5" : 0, "CountFor6" : 0, "CountFor7" : 0, "CountFor8" : 0, "CountFor9" : 0 }
- Save the flow and keep saving at regular interval.
- Add another variable that will be treated as counter for the how many Random Numbers, we will generate.
- Now, add another action called Do-Until and set the validation criteria. Here, we are generating 10 random numbers.
- Now inside the Do-until action, add a Compose action and add the expression-
- rand(0,9)
- Now, add a Switch action and for each number from 0 to 9, add Case. The output of Compose action will be the "On" criteria for the Switch action.
- Now for each case, we will add the increment action. However, it will be little bit different. First add another compose action and add the logic mentioned below.
setProperty(variables('objCounts'),'CountFor0',add(int(variables('objCounts')['CountFor0']),1))
- Then add another action named "Set variable" and assign the output of above Compose action to the variable "ObjCounts". this will update the variable data.
-
- The complete code setup for Case 0 will be-
- The same steps we need to repeat for rest of the cases. First add a Compose action then add Set Variable action.
- Now, we will add action to increment the counter so that the loop gets terminated once the counter reaches to 10.
- Now, if you wish to check if the logic we written over here worked, let's assign the ObjCounts variable to a Compose action. This action will be added outside the Do-Until loop.
- Save the workflow and run it to verify.
- If you wish to know, what all random numbers were generated, then you may add an action called "Append to array variable" as below to add all those numbers in an array-
- Lastly, add another compose action to see what all numbers got generated.
- Run the flow again and verify the result.
- This time the outcome is-
- This is how, you can leverage the beauty of Object variables.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.