Quantcast
Channel: blog.atwork.at
Viewing all articles
Browse latest Browse all 1118

Work with items in Apply to each in Power Automate or Azure Logic Apps

0
0

Automation of small tasks is helpful for many recurring processes. Unless you regularly use Power Automate (Flow) or Azure Logic Apps, it is often not that easy to remember the correct syntax for accessing items in a loop. Here is the quick help.

This sample is using a flow in Power Automate to read messages from a Microsoft Teams team (I know, this sounds strange) and outputs any field to an email. The purpose is just to see how to access data in JSON format in a "Apply to each" loop. Here´s the screenshot of the complete flow with the description of the actions.

  • We simulate an event by calling an HTTP trigger (e.g. with Postman, curl, PowerShell or similar).
  • We initialize a variable "list" of type "Array".
  • Then, we use the Teams "Get messages" action and select a team and a channel.

image

  • To access the messages, we use the control "Apply to each" where we use the output of the "Get messages" action, as here.
  • In the loop, we simply append a specific value to the list object.
  • To see what elements we get, we can run the flow and see what data is delivered (see below).

So, here´s the loop-action, including a variable action.

image

The "Get messages" shows the data. To access, let´s say, the displayName of the user who wrote the post, we can follow the path as shown below and use the following syntax in the loop:

items('Apply_to_each')['from']?['user']?['displayName']

We access the action with it´s name "Apply to each" and select the "from" key. In that key, there is a "user" object nested. In the "user", there are three more keys nested: id, displayName and userIdentityType. This is how we can navigate through the JSON document.

image

The "Append to array variable" action only allows to add a value, but fails if the expression is empty. If, for whatever reason, no value is existing, the action throws an error. To work around this, we can use a little trick an add a space and remove that with the trim function later. So, we can use such an expression to avoid any errors.

trim(concat(items('Apply_to_each')['from']?['user']?['displayName'],' '))

image

So, every user name is added to the list array.

  • The last step is to output the collected list data as an email to a developer account - just to show the data if needed.

image

As result, the array is sent in the body of an email, as here:

list items:
["John Doe","","Anne Doe",…]

That´s it. The purpose of this small article was to show the syntax how to work with items in a loop. Hope this small guidance helps for working with flows.


Viewing all articles
Browse latest Browse all 1118

Latest Images

Trending Articles