Variables are generic objects that can store things for us. They are instrumental, but we need to provide them with data. To do that, we can use the “set variable” action.
Let’s explore it and see how it works.
Where to find it?
To find it, you can search for “Set variable” action or go to “Built-in”:
You’ll probably need to expand to see the “Variable” group.
Then select the “set variable” action.
Here’s what it looks like.
Now that we know how to find it let’s understand how to use it.
To use the “set variable” action, you first need to use the “Initialize variable” action to create a variable. After that, you’ll see it in the dropdown by the name you defined.
(I know that the title “number” doesn’t make sense with the variable. I want to show you what the function looks like without picking any value since the boolean is the default one)
Now that we have a link to the variable let’s add data. It’s important before we do that to understand what data we can add. In the example above, since we defined the variable as a “boolean,” we can only use the “true” function or “false” function. Flow is smart enough, in this case, to recognize the “true” string as a boolean value, but you should not rely on this because it doesn’t happen everywhere (for example, in the “Initialize variable” action). Here’s an example:
Power Automate returns:
Now let’s define the variable as a number. Next, let’s see what we can add to it.
Since the value is an “Integer,” it will work.
How about if we define it as a “Float”?
Power Automate will return an error since a “Float” is not an “Integer,” Power Automate will return an error. The same goes for a string.
Or for anything else you try to provide it.
Please note that this validation will only be performed if Power Automate determines the value type. So, for example, if you use the value of another variable, Power Automate can understand that the other variable is for a specific type and evaluate it. But if you’re using something else, like a “Get item” from a SharePoint list, then Power Automate will only validate the value once it’s running.
It will return the following exception:
The variable ‘Number of people above 50’ of type ‘Integer’ cannot be initialized or updated with value of type ‘String’. The variable ‘Number of people above 50’ only supports values of types ‘Integer’.
It’s essential to understand what value is added to the variable and deal with the exception if there’s an issue.
It’s not a defined limitation, but I consider it one that Power Automate doesn’t validate objects that it could do it. For example:
Power Automate will only display “Integer” parameters, knowing the types. I hope that Microsoft improves this since variables are used a lot, which would avoid multiple issues.
Here are some things to keep in mind.
Name it correctly
It’s essential to name the “set variable” action correctly because we need to understand why we’re doing it. For example, having “Set the number of people above 50 with the calculation result” will tell you a lot of information, even without expanding the action. This will make a massive difference for complex Flows when you’re trying to debug something.
Always add a comment.
Adding a comment will also help avoid mistakes. The name can indicate the intention, but the comment can provide additional information. Remember that you can add formulas in the “set variable” action, so you can process the data before adding it to the variable. A commend explaining the process goes a long way to avoid mistakes and help properly debug your Flow.
Always deal with errors.
Have your Flow fail graciously and notify someone that something failed. It’s horrible to have failing Flows in Power Automate since they may go unlooked-for a while or generate even worse errors. I have a template, and a template that you can use that will help you make your Flow resistant to issues. You can check all details here.
Back to the Power Automate Action Reference.