Saturday, July 10, 2021

Logic Apps : For Each Inside a For Each - Fetching values from field in an array inside an array

 

Introduction

In last post Debatching(Splitting) XML Message in Logic Apps - ForEach and SplitOn  and we understood about how For each can be used to split/debatch messages.

In above scenario  we are using only one for each, whereas there can be situations where more than one For each would be required to solve the problem.


Scenario 


Say below is the purchase order and we need to take some action based on productName (value to be fetched). 

So we have two arrays to deal with
1. Products (having multiple product)
2. Product (having single product detail) within each products

PO Sample


Creating Logic App


Create a http based triggered logic app, followed by initializing a variable (in it we store value of productName field)

initialize a variable


Next is to add first For Each which will iterate through array of Products, input to for each has to be an array and to do that we use xpath query as can be seen in below image

first for each loop



Using xpath above - we are asking to traverse through the triggerBody and look out of products node and if found add entry in  For each array.(All occurrence of products nodes are added).


xpath(xml(triggerbody()),'purchaseOrder/products')


The array will have all the products node , so if input has 10 Products node then the array formed using xpath will have 10 items in it, each representing a single products node.


second for each loop


Next, we need to loop through each product from products and for that we will use array which was created in first For each and for that syntax is - items('For_each') and using xpath - we are asking to traverse through the items('For_each') and look out of product node and if found add entry in  For each array.(All occurrence of product nodes are added).

xpath(xml(items('For_each')),'products/product')

fetch the element value

last step is to use Set Variable  action where we fetch value of product name from each product node, and for that we will use array which was created in second  For each and for that syntax is - items('For_each_2') and using xpath - we get the current element value.


                xpath(xml(items('For_each_2')),'product/productName/text()')[0]











No comments:

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment