The “append to array variable” action is one of the most used functions on my daily Flows. It’s instrumental when parsing information and keeping the parsed information for later.
Let’s explore it and see how it works.
Where to find it?
To find it, you can search for “Append to array variable” action or go to “Built-in”:
You won’t probably see it in the main options, so you’ll need to expand until you dine the “Variable” group.
Select the “append to string variable” action.
Here’s what it looks like:
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.
To use the “append to string 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.
We define the variable as an “Array” since the action will work only on “Arrays.” If we have another “Integer” variable, for example:
We won’t see it in the options.
Let’s try it with a simple example. Let’s append a string to the array.
The compose action will contain the values in the array to check them. Here’s the result:
Looks good. But what happens when we “mix” values. For example, let’s now add an “Integer.”
Here’s the result.
As you can see, we can add mixed types, and they keep their data type. As you notice above, the second parameter doesn’t have double quotes, meaning that it’s an integer.
It makes sense that it’s allowed since it’s an “array,” meaning that it is a “bucket” where you can dump values, but it’s pretty dangerous if you don’t ensure that the values are always the same. For example, let’s say that we have an array of numbers, and we’re multiplying them to get a new number. The Flow will return an error if one array row is a string. Of course, it can even be a string with only numeric characters, but to Power Automate, it’s a string nevertheless.
Always convert and control the value that is being appended in action. The result must be deterministic; otherwise, you’ll get ugly results like the above or even errors.
Similarly, we have a mixed blessing and curse with the “append to string variable” action. Having a mixed bag of types will result in issues and breaking Flows. Even if Microsoft has to keep the “append to array variable” action as is so as not to break compatibility, it will help a lot of people if either:
- The action would have an additional parameter with the definition of the type
- For example, have another action called “append to an array variable with type” action.
It would force everyone to keep in mind the values by automatically validating the values inserted in action.
Here are some things to keep in mind.
Since we don’t have a type defined, make sure the type is correct.
Since we can’t force that the array only contains “Integers,” for example, we can validate before if the value that we’re going to insert is one.
Validate every value into the array and ensure that they are always the same type. If not, you’ll have breaking Flows when the type is incompatible with any other activity that uses the rows in the array.
Name it correctly
The name is super important since the operation is not very descriptive. For example, having “append to array variable” won’t tell us what kind of value we are appending, the desired result, and the array that is being appended. It would be best to use the comments to provide further information, but the name is used to know what the action is doing without expanding it.
Always add a comment.
As explained before, it’s essential to know a few things:
- First, what is the desired output with the append?
- Why are we appending?
- What are we appending?
- What is the type we want to append?
The intention of the action may not be the same; as a result, so we need to define it before we start working on it. You will then know what was intended when debugging a Flow if something goes wrong.
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.
Photo by Ben Harritt on Unsplash