Power Automate: use own Array to loop actions

Why?

Sometimes you want to do the same set of actions on a variable number of items AND each item will have its own variable values for properties… instead of using a Switch control where you would have to define the variable options of items, we will create our own Array and use that Array to make an efficiently manageable Flow 💪.

What?

A post on my lessons learned when trying to create a Flow with as less actions as possible, performing the same set on actions on a variable number of items with their own variable values for properties.

How?

You may need to get some basic background information on Arrays and JSON within Power Automate Flows. If these concepts are not yet familiar to you, please search around the internet for extensive information.

My summary: within Power Automate Flows we use JSON (JavaScript Object Notation) to show multiple objects (items with properties) in an Array. We can use the Array with a variable number of items to loop through each item and perform a set of actions on each item. (when you use an Array item propery in one of your actions, that action will be placed in an Apply to each loop: can be annoying so please vote on the idea to popup a confirmation before it is automatically done: https://powerusers.microsoft.com/t5/Power-Automate-Ideas/Confirm-before-add-Apply-to-each/idc-p/522322#M20383 🤓)

Example scenario: we receive an email in our inbox and we want to get a set of values that we know are present in the string of the body. The emailbody will have a consistent body like:

||UserID||||forward||
||Name||||Service Account - Sharepoint Migration||
||Telephone||||||
||Email||||srv-sharepointmig@tenant.onmicrosoft.com||
||Department||||||
||DateOfChange||||2020-04-09||
||RequestText||||Request about Help FAQ||

1) We initialize our own Array Variable: varArrayStrings to represent the set of strings we want to check for:

Notice that this Array contains objects in JSON format and each object has multiple properties: ArrayIndex, KeyToFind and ValueFound. The ArrayIndex is for clarification, we will use the KeyToFind for this scenario and ValueFound is for a scenario I will explain in the next blog post ⏳.

2) To make this flow reusable I put the body of the email into a string through a Compose action:

We will also use the same type of Compose action to define the characters that separate the data within our text. In my example is it a double pipeline character “||” because that is something I never see anyone use in normal text:

Placing these data in a Compose action will make it reusable in other actions while managing the character centrally (in case it should ever change or you want to reuse the same Flow for a different scenario).

3) When we place our variable varArrayStrings inside an Apply to each loop:

we can use every property with a formula like:

items('Apply_to_each_KeyToFind')?['KeyToFind']

4) We could then use two actions (with could be combined to one) to find the information we want in the email body:

the first action you can just copy-paste because of the @{} inclusion and it just creates the string of the key we want to find in the email body:

@{items('Apply_to_each_KeyToFind')?['KeyToFind']}@{outputs('Compose_-_FullStringKeyValueSeparator')}@{outputs('Compose_-_FullStringKeyValueSeparator')}

the second action you need to place as an Expression and it finds the value next to key we wanted to find:

substring(substring(outputs('Compose_-_FullString'),add(lastIndexOf(outputs('Compose_-_FullString'),outputs('Compose_-_FullStringKeyToFindArray')),length(outputs('Compose_-_FullStringKeyToFindArray')))),0,IndexOf(substring(outputs('Compose_-_FullString'),add(lastIndexOf(outputs('Compose_-_FullString'),outputs('Compose_-_FullStringKeyToFindArray')),length(outputs('Compose_-_FullStringKeyToFindArray')))),outputs('Compose_-_FullStringKeyValueSeparator')))

Resulting in:

Wait for my next post to show, how we can then use the found value of the properties outside the Apply to each loop 😎

5 thoughts on “Power Automate: use own Array to loop actions

  1. Hii,
    i want to read data from in this format from text fle
    StudId Name Stream
    001 John Science
    002 Mike Commerce

    we can do modification in format like add comma or separator instead of white space(but row need to be identify)

    can you please give any reference or idea

    Regards.

  2. Common usage to get an item from parsed text: split(outputs(‘bodyArray’)[1],’:’)
    How can this be done?
    split(outputs(‘bodyArray’)[INDEX],’:’)
    and better
    split(outputs(‘bodyArray’)[INDEX-1],’:’)
    Given that out of bounds is not an issue. This scenario arises when the INDEX to an item in an array is known and the items relative to it needs to be retrieved using the known INDEX.

Leave a comment