Wednesday, August 24, 2022

Power Automate: Working With OBJECT Variable

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.

  1. Open the Power Automate Portal. Click on "+ Create", select "Instant cloud flow".
  2. Give your flow a suitable name. and select the appropriate trigger condition. For demo purpose, I am selecting "Manually trigger a flow".
  3. Click on "Create" to create the flow. Now, we will add 2 variables
    1. arrRandomNumbers (variable type - Array)
    2. objCounts (variable type - Object)
  4. 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).
    1. {
      	"CountFor0" : 0,
      	"CountFor1" : 0,
      	"CountFor2" : 0,
      	"CountFor3" : 0,
      	"CountFor4" : 0,
      	"CountFor5" : 0,
      	"CountFor6" : 0,
      	"CountFor7" : 0,
      	"CountFor8" : 0,
      	"CountFor9" : 0
      }
      
  5. Save the flow and keep saving at regular interval.
  6. Add another variable that will be treated as counter for the how many Random Numbers, we will generate.
  7. Now, add another action called Do-Until and set the validation criteria. Here, we are generating 10 random numbers.
  8. Now inside the Do-until action, add a Compose action and add the expression-
    1. rand(0,9)
  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.

  10. 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.
  11. setProperty(variables('objCounts'),'CountFor0',add(int(variables('objCounts')['CountFor0']),1))
    
  12. 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.
  13.  
  14. The complete code setup for Case 0 will be-
  15. The same steps we need to repeat for rest of the cases. First add a Compose action then add Set Variable action.
  16. Now, we will add action to increment the counter so that the loop gets terminated once the counter reaches to 10.
  17. 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.
  18. Save the workflow and run it to verify.
  19. 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-
  20. Lastly, add another compose action to see what all numbers got generated.
  21. Run the flow again and verify the result.
  22. This time the outcome is-
  23. This is how, you can leverage the beauty of Object variables.

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.