Microsoft Flow – 2 methods to not use Apply to Each action when only a single filtered item is expected

1+

In this post I will show you 2 methods when using Apply to each Flow action is not necessary. By not using them you make your Flows look cleaner, less complicated.

Please note, these methods are mostly good when only a single item is expected after filtering an array.

I created a SharePoint List with two text columns – Title and Color. Both columns are required. Title values are set to be unique (!).

When using Get Items action, an array of items is returned regardless if it’s a single item in it or not. The moment you try to use a Condition action on its result, the system will insert an Apply to Each action to go through every single possible item. Again, it’s redundant when by design a single item is returned after filtering.

In my test flow I’m using a very simple Filter Query on purpose – Title eq ‘Test 1’. In a real life situation that value you are filtering by can come from any other source and should make more sense.


The Flow without using Apply to Each action:

Method 1 uses a First function. The code for Condition action:

@equals(first(body('Get_items')?['value'])?['Color'], 'Blue')

Method 2 uses ?[0] to get the first item in the array. The code for Condition action:

@equals(body('Get_items')?['value']?[0]?['Color'], 'Blue')

Both of these expressions above are for when Edit in advanced mode is selected for Condition actions.

1+

Leave a Reply

Close Menu