Mastering Power Automate: The createArray() Function Explained
Power Automate offers powerful data manipulation capabilities, enabling users to work with collections of data efficiently. A cornerstone of this is the createArray() function, which allows multiple values to be stored and processed as a single array. This is invaluable when dealing with SharePoint lists, Dataverse records, Excel rows, API responses, email attachments, and dynamic content. Instead of managing individual values separately, arrays enable you to process multiple items collectively.
The createArray() function is designed to create an array containing multiple elements within a single expression. In this article, we will explore how to effectively create and utilize array functions in Power Automate.
What is an Array?
An array is essentially a collection of values stored within a single variable. For example, instead of having:
- Employee1
- Employee2
- Employee3
- Employee4
You can represent this as a single array:
[ "Employee1", "Employee2", "Employee3", "Employee4" ]
Arrays help organize and process multiple values much more efficiently.
Why Use Arrays?
Arrays are particularly useful in various common business scenarios:
- Email Lists
- Employee Records
- Product Lists
- SharePoint Items
- Dataverse Rows
- API Responses
The benefits of using arrays include:
- Store Multiple Values: Consolidate numerous data points into a single variable.
- Process Data Efficiently: Iterate and manipulate collections of data with ease.
- Reduce Complexity: Simplify your flow logic by handling groups of data.
- Improve Automation: Build more robust and dynamic automated processes.
Business Scenario Example
Imagine you need to send an email to multiple users. Instead of managing individual email addresses, you can create a single array containing all the emails. This array can then be used in a loop to send notifications to each user.
Understanding createArray()
The syntax for the createArray() function is straightforward:
createArray( value1, value2, value3 // ... more values )
For instance:
createArray( 'John', 'David', 'Smith' )
This expression will output the following array:
[ "John", "David", "Smith" ]
The createArray() function allows multiple values to be combined into a single, manageable array.
Step-by-Step Guide to Creating an Array in Power Automate
Step 1: Create an Instant Flow
- Navigate to make.powerautomate.com.
- Click on Create.
- Select Instant cloud flow.
- Choose Manually trigger a flow as the trigger.
Step 2: Add a Compose Action
- Add a new action and search for Compose.
- In the Inputs field, enter the
createArray()expression. For example:createArray( 'Employee1', 'Employee2', 'Employee3' )
The output of this action will be:
[ "Employee1", "Employee2", "Employee3" ]
Step 3: Test the Flow
- Save your flow.
- Click Test and then Manually.
- Run the flow.
You should see the output confirming that the array was created successfully.
Examples of Using createArray()
Example 1: Create an Email Array
This is commonly used for sending email notifications to multiple recipients.
createArray( 'user1@company.com', 'user2@company.com', 'user3@company.com' )
Output:
[ "user1@company.com", "user2@company.com", "user3@company.com" ]
Example 2: Create a Number Array
Useful for calculations and reporting purposes.
createArray( 100, 200, 300, 400 )
Output:
[ 100, 200, 300, 400 ]
Example 3: Create an Array from Variables
This approach is commonly used when combining dynamic values from different variables.
First, initialize two variables:
- Person1: 'Alice'
- Person2: 'Bob'
Then, use the createArray() function:
createArray(
variables('Person1'),
variables('Person2')
)
Output:
[ "Alice", "Bob" ]
Example 4: Create an Array of Arrays (Nested Array)
You can create nested arrays using createArray(), which can be useful for complex data structures.
Initialize two array variables:
- Questions: [1,2,3,4,5]
- Answers: [2,4,1,5,3]
Expression:
createArray(
variables('Questions'),
variables('Answers')
)
Result:
[ [1,2,3,4,5], [2,4,1,5,3] ]
Using Array Variables
Power Automate supports array variables, which are ideal for storing collections of data dynamically.
- Initialize Variable: Set the Type to Array and provide an initial value. For example:
- Name:
EmployeeArray - Type:
Array - Value:
["John", "David"]
- Name:
- Append to array variable: You can add new items to an existing array variable. For instance, appending "Smith" to
EmployeeArraywould result in:["John", "David", "Smith"]. This is useful when building arrays dynamically during flow execution.
Looping Through Arrays
The Apply to each control is the standard way to iterate through the items in an array. You can use the output of a createArray() function or an array variable as the input for the Apply to each loop. Inside the loop, you can then perform actions on each individual item.
Real-World Example: Employee Notification System
Consider an employee list: John, David, Smith, Chris. A Power Automate flow can:
- Use
createArray()to put these names into an array. - Use Apply to each to loop through the array.
- Inside the loop, send an email to each employee.
This results in emails being sent to all employees efficiently.
SharePoint Example
When working with multiple SharePoint items, you can create an array of item identifiers or relevant data. For instance, if you have tasks like 'Task1', 'Task2', 'Task3':
- Create an array:
createArray('Task1', 'Task2', 'Task3') - Use Apply to each to loop through this array.
- Within the loop, you could create corresponding records in another SharePoint list or perform other actions based on each task.
This is useful for processing multiple SharePoint records in a structured manner.
Common Array Functions in Power Automate
createArray(): Creates an array with specified values.array(): Converts a single value into an array. This is distinct fromcreateArray(), which can handle multiple values in one go.length(): Returns the number of items in an array.join(): Combines all values in an array into a single text string, separated by a specified delimiter.Filter array: Filters the items in an array based on specified conditions.
Workflow Summary
A typical workflow involving arrays might look like this:
- Data Acquisition: Gather data from various sources.
- Create Array: Use
createArray()or other methods to form an array. - Store Values: Optionally store the array in a variable.
- Apply to Each: Loop through the array items.
- Process Records: Perform actions on each item.
- Output Result: Generate the final output or update systems.
Best Practices
- Use Arrays for Multiple Values: Avoid storing multiple records or related data within single strings.
- Leverage
createArray()for Multiple Items: It's the best function for combining several distinct values into an array. - Use Array Variables: Employ array variables when data needs to be built or modified dynamically during flow execution.
- Avoid Unnecessary Loops: Process only the data that is required to maintain efficiency.
- Validate Empty Arrays: Before processing, check if an array is empty to prevent errors.
Common Challenges
- Empty Array: Occurs when no values are supplied to
createArray()or when a data source returns no results. - Wrong Data Type: Attempting to use a string or other data type where an array is expected.
Apply to EachErrors: Often caused by an incorrect array reference or passing a non-array value to the loop.- Nested Arrays: May require additional logic or nested loops to access inner elements.
Benefits of Using Arrays
- Better Data Management: Organize and structure multiple values effectively.
- Easier Automation: Process collections of data efficiently, reducing repetitive logic.
- Improved Performance: Streamlined logic can lead to faster flow execution.
- Dynamic Workflows: Adapt to changing data requirements and sources.
- Scalable Solutions: Build robust automations suitable for enterprise-level needs.
Conclusion
The createArray() function is a fundamental and highly useful expression in Power Automate for managing collections of data. Whether you are working with email lists, SharePoint items, Dataverse records, Excel rows, or API responses, arrays simplify automation and enhance flow efficiency.
By effectively combining createArray(), array variables, Append to array variable actions, and Apply to each loops, developers can construct scalable and dynamic Power Automate solutions that efficiently process large amounts of data. The createArray() function is specifically designed to create arrays with multiple values, offering greater flexibility than the array() function for many common business scenarios.