Power Platform:Power Automate – Get the next item in Apply to each loop

Hello Everyone,

Business workflows have become easier since Power Automate Flows are with us. Power Automate lets us build complex functionalities easily. But, sometimes we need some functionality and if it is not readily available so we have to find workaround for that.

Background:

In one of the flow I was working on, I wanted to perform an operation on first item, if output of first item match with the condition then execute Yes block else execute No block i.e. to get next item and perform operation on that. So, let’s see how to do it!!

We are going to use following functions combinely:

  1. add(number1, number2) :- to carry out sum of integer
  2. skip([<collection>], <count>) :- it removes items from front of collection and returns the remaining collection of items. <count> is no. of items to remove from front of collection.
  3. first(‘<collection>’) or first([<collection>]) :- it returns first element of collection

Steps:

  1. Initialize a variable of number, set its value to zero as this variable we are going to use as index of item.
  2. Use the following expression:
first(skip(Collection,add(variables('index'),1)))

3. If you look at the expression, first of all add() is carried out, then skip() and finally first(). Have a look at following example:

// Collection is [25,40,30,54,87,9]
// Initially index is set to 0

add(0,1) = 1
skip([25,40,30,54,87,9], 1)    // 1 is output of add(0,1) 

//skip() will remove one element from the array or collection
first([40,30,54,87,9])

// finally combining above functions
first(skip([25,40,30,54,87,9],add(variables('index'),1)))  // output is 40

4. For arrays it looks easy but this expression plays a great role when retrieving SP list item data.

5. If you want SP list item value for any particular column then you can use like below:

first(skip(Collection,add(variables('index'),1)))?['<internal-column-name>']

In my case, collection was nothing but the output of Get items action so I used value as collection and in place of <internal-column-name> I used AssignedTo/Email, so expression comes out to be.

first(skip(outputs('Get_items')?['body/value'],add(variables('index'),1)))?['AssignedTo/Email']

References:

Thanks for reading.

Please like, share, comment.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: