October 16, 2024

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.

Pick “Variable.”

Then select the “set variable” action.

Here’s what it looks like.

Pro Tip:
Power Automate tends to save the most common actions in the main screen, so check there before going through the complete hierarchy. Also, you can use the search to find it quickly.

Now that we know how to find it let’s understand how to use it.

Usage

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.

Limitations

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 knows that the “Title” is a string.So wee can simulate this by adding another “Get item” from a SharePoint list and checking the “Dynamic Content” tab:

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.

Recommendations

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.

Am I missing something? Leave a comment or interact on Twitter. Let’s work together to make this reference as complete as we can

Back to the Power Automate Action Reference.

Photo by Customerbox on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon