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, but today we’ll explore a more specific way to add to a variable. The “append to string variable” action.
Let’s explore it and see how it works.
Where to find it?
To find it, you can search for “Append to string 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:
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 a “String” since the action will work only on “Strings.” So if we have another “Integer” variable, for example:
We won’t see it in the options.
As we mentioned before, the “append to string variable” action is a specialized “set variable” action, where it combines it with the “Concat function.” So if we have the variable with “Manuel” and use the “append to a string variable,” the string “ Gomes.”
The “append to string variable” will always add to the end of the variable’s string value, including spaces or any other character.
So what if we append an Integer value?
Power Automate will return:
Nice that Power Automate converts the values automatically for us. But this doesn’t mean that the values are always converted correctly. Let’s look at an array, for example. We can use the “createArray” function to add to the “append to string variable” action and see what happens.
Using the formula:
createArray(' T ', 'Gomes')
Here’s what we get.
Power Automate converts the array as a JSON string. Would it be nice to have “Manuel T Gomes” instead, right? To do that, you can use the “join function” to get that result. Replace the function with:
join(createArray(' T ', 'Gomes'),'')
Here’s what you get:
Always convert and control the string that is being appended in action. The result must be deterministic; otherwise, you’ll get ugly results like above.
Having Power Automate converting the values automatically can be a blessing and a curse. It’s not a limitation per se, but I would like it to restrict the parameters you can pick to only “String” objects. It would force the users to think and convert the values manually and be much more in control of what it’s happening. Leaving it like this will make people believe that Power Automate will always convert it correctly, but, as we’ve seen before, the correct definition can be different from person to person.
So what I’m saying is that a limitation is missing :).
Here are some things to keep in mind.
If you use anything other than a string, convert it first
Although Power Automate will convert the types and appends them, we should fully control how the data is converted. For example, think of a date using the “utcNow function.”
The output is the following.
Notice that the data was converted using the default format, but you may want it to convert in a different form. Also, notice that we have an “ugly string” without a space between the name and the date. But having something like “Manuel started on the 13th of December” would be a lot nicer.
The point is to control the process and use the append-only to add a string at the end.
Name it correctly
The name is super important since the operation is not very descriptive. For example, having “append to string variable” won’t tell us what kind of string, the desired result, and the string that is being appended. It would help if you used 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?
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.