Learn About the Differences Between Three Workflows List Functions: For Each, Map, and Reduce
Okta Workflows is a no-code platform for automating identity processes.
This blog post is based on a question asked during office hours or the #okta-workflows channel in MacAdmins Slack: What are the differences between three List functions: For Each, Map, and Reduce?
Getting started
The List function has three cards to iterate over a list:
- For Each – processes each item in a list with a helper flow. Helper flow doesn’t return a value
- Map – processes each item in a list with a helper flow. Creates a new list with modified items. A new list is returned to the main flow
- Reduce – process each item in a list with a helper flow. Reduces the list to a single value that is returned to the main flow
Let’s look at every function with an example.
List – For Each
The For Each card processes each item in the list with a helper flow.
You have a list of users and need to process each user. For example, de-provision each user, cancel their subscription, update their profile, or send them an email.
The For Each card doesn’t return output.
Here is an example.
Using For Each card, main flow
In the screenshot above:
- The Assign card sets the group ID
- List Group Members card lists all users in a group
- For Each card calls a helper flow for each user in a group. The User object is passed to the helper flow
This is the helper flow:
For Each card helper flow
In the helper flow:
- The Helper Flow card defines the flow input. In this example, it is the User object
- Using the Get Multiple card, you retrieve specific fields from the User object.
- Next, you process each user. For example, de-provision each user, cancel their subscription, update their profile, or send them an email. In this short example, you are displaying the values using the Compose card
In summary, the For Each card processes each item in a list with a helper flow but doesn’t return a value back to the main flow.
Learn how to use the List – For Each card
How to Setup a Workflows Helper Flow (With For-Each and Streaming Cards)
List – Map
The Map card processes each item in the list with a helper flow. For each item in the list, the Map card adds a new value to a new list. The new list is returned to the main flow.
You have a list of users and need a new list where each user is listed as lastname.firstname.
Using Map card, main flow
In the screenshot above:
- The Assign card sets the group ID
- List Group Members card lists all users in a group
- Map card calls a helper flow for each user in a group. The User object is passed to the helper flow
- The Map card returns a list (Last.First List). This is different from the For Each card that doesn’t return a value
- The Assign card is used to display the returned list
This is the helper flow:
Using the Map card, helper flow
In the helper flow:
- The Helper Flow card defines the flow input. In this example, it is the User object
- Using the Get Multiple card, you can retrieve specific fields from the User object. In this flow, you retrieve the ID, First Name, and Last Name.
- Using the Concatenate card, you create lastname.firstname name format for each user in the list
- Each new name (lastname.firstname) is returned to the main flow. This puts the new name into a list
This screenshot shows one helper flow run:
Using the Map card, helper flow run
In summary, the Map card processes each item in a list with a helper flow. Creates a new list with modified items. A new list is returned to the main flow.
Learn how to use the List – Map card
How to Iterate Over Items and Create a New List (or How to Use the List – Map Card)
List – Reduce
The Reduce card processes each item in the list with a helper flow. The card reduces the list to a single value that is returned to the main flow.
Using the Reduce card, main flow
In the screenshot above:
- The Assign card sets the group ID. It also sets a text variable Memo
- List Group Members card lists all users in a group
The is the helper flow:
Using the Reduce card, helper flow
In the helper flow:
- The Helper Flow card defines the flow input. In this example, it is the User object and the required memo variable
- Using the Get Multiple card, you can retrieve specific fields from the User object. In this flow, you retrieve the Email, First Name, and Last Name.
- The Compose card, adds new user information to the previous message
- The Return card, returns the newly created message that is then passed again to the helper flow (until all users are processed)
On the 1st run, memo is set to People list:
The Return card returns:
People list: Max Katz ([email protected])
On the 2nd run, memo is set to the above message and the Return card returns:
People list: Max Katz ([email protected]) Stella Green ([email protected])
On the 3rd run, memo is set to the above message and the Return card returns:
People list: Max Katz ([email protected]) Stella Green ([email protected]) Olivia Green ([email protected])
On the 4th run, memo is set to the above message and the Return card returns:
People list: Max Katz ([email protected]) Stella Green ([email protected]) Olivia Green ([email protected]) Alicia Web ([email protected])
And, on the 5th run, memo is set to the above message and the Return card returns:
People list: Max Katz ([email protected]) Stella Green ([email protected]) Olivia Green ([email protected]) Alicia Web ([email protected]) Jacqueline Miles ([email protected])
Now that the Reduce card processed all the items, it reduced the list to a single item (the combined message) and that item is returned in the Message property.
List – Reduce card
In summary, the Reduce card processes each item in a list with a helper flow. The card reduces the list to a single value that is returned to the main flow.
Learn how to use the List – Reduce card
How to Reduce a List to a Single Value, 3 Examples Using List-Reduce Workflows Card
Video
Watch a short video about these flows.
Summary
In this blog post, you learned how to use three List actions:
- For Each – processes each item in a list with a helper flow. Helper flow doesn’t return a value
- Map – processes each item in a list with a helper flow. Creates a new list with modified items. A new list is returned to the main flow
- Reduce – process each item in a list with a helper flow. Reduces the list to a single value that is returned to the main flow
Get your Workflows questions answered
Do you have a question about Okta Workflows? Not sure how to build a flow? Join the weekly community office hours to get help.