I see many questions around this function; one of those is, “why should I use a createArray function?” It may seem strange, but this function is handy instead of the “append to array variable action.” First of all, you have many values; you’ll have an endless list of actions when you can do everything in one action. I’m all for having the work divided by multiple actions, but it will be more harm than good in this case.

Let’s explore it a bit further.

Usage

You need to provide one or more objects to the function, and it will return an array of objects. Notice that we’re keeping things generic here so that you can provide any number of objects of different types. If you’re a developer, this may be counterintuitive because most platforms or programming languages require an array of a specific type (string, int, etc.).

Let’s start with a simple example, where the variable is a Boolean value:

createArray('Manuel',variables('TEST'))

will return 

[
  "Manuel",
  false
]

Having distinct items is possible because this action stores a JSON, to the syntax above should be familiar.

Integer and Floats

Let’s look at an example where the variable is an Integer.

createArray('Manuel',variables('TEST'))

will return 

[
  "Manuel",
  12
]

And a Float.

createArray('Manuel',variables('TEST'))

will return 

[
  "Manuel",
  12.5
]

All as expected.

Arrays

Now a more tricky example. When you provide an array, what will it do? Create an array with the provided array or an array inside an array? In a JSON format where []:

Variable array:
['Teixeira ','Gomes']

createArray('Manuel',variables('TEST'))

will we get:

['Manuel','Teixeira ','Gomes']

or 

[
	'Manuel',
	['Teixeira ','Gomes']
]

Let’s check. Same as before:

createArray('Manuel',variables('TEST'))

will return 

[
  "Manuel",
  [
    "Teixeira ",
    "Gomes"
  ]
]

The createArray function always creates a new array even if we provide an array as one of the objects.

Limitations

This is not a limitation, but Microsoft’s documentation defines the type of objects you provide to the createArray function as “Any, but not mixed.” As you’ve seen above, you can mix and match distinct types of values, so I think this is either an old behavior or a mistake. You can mix different types of objects.

Recommendations:

Here are some things to keep in mind.

I prefer this function over the array function.

The array function also exists in Power Automate, but it serves a different purpose. The array function converts only one value into an array. With the createArray function, we’re creating an array with multiple values. Although it’s not the same thing, the result is roughly the same. With the createArray function, we get the added value of adding multiple values, so, in my opinion, it is preferable to the array function. Only use the array function if you want to signal that you’re converting a value from another type to an array.

Don’t use multiple “append to array” actions.

As we’ve seen above, this function doesn’t append one array into another, so always be aware and sure of what you’re creating.

Don’t mix data types.

You can add multiple data types, and it will return an array of heterogeneous objects. Although this is quite powerful, for example, if you want to generate a JSON with a defined list of values, it should be used very sporadically.

There’s a good reason that most programming languages require that the types are the same. Although some of the reasons are due to their implementation, it’s quite “safe” to parse arrays since you always expect the same value. Keeping the same datatype is, however, a best practice (see next section) and not enforced by the platform.

Always check the types of the elements before parsing the array

Although it’s not directly related to the createArray function, it’s important to have this in mind. Always check the data types of the array returned by this function, or an array for that matter.

Checking the data can be tricky, but you can have issues if you don’t do it. If you’re expecting an array of Integers, there’s no limitation or reason why the array could not include strings, for example. Always check the values before using them; otherwise, your Flow will crash, or you’ll be using invalid data.

Don’t nest unless the function you want nested arrays

As we’ve seen in the example above, if you nest createArray functions, you’ll get nested arrays. If this is what you need, please go ahead, but be careful when nesting.

Localization

Please note that formulas may have localization-based differences. For example, you should write the createArray function with “,” separating each of the arguments, but if your regional settings are set to Portugal, you should use “;” instead.

Sources:

Microsoft’s createArray Function Reference

Back to the Power Automate Function Reference.

Photo by Daniele Levis Pelusi 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 *