For Each Scope In MuleSoft (Mule 4)

Mulesoft

The For Each scope splits a payload into elements and processes them one by one through the components that you place in the scope. It is similar to a for-each/for loop code block in most programming languages and can process any collection, including lists and arrays. The collection can be any supported content type, such as application/json, application/java, or application/xml.

Following are some key points regarding For Each scope

  • For non-Java collections, such as XML or JSON, use a DataWeave expression to split data. Use the Collection field for this purpose. If it’s a Java collection then it will split the payload by default.
  • For each work in sequential only. So if there is any error processing any record then it will not process the further records and break the look immediately
  • For Each does not modify the current payload. The original payload is returned after processing is completed.
Create mule application which will the XML collection as payload
<?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>

Add a logger which will print the received payload

Now add For Each activity

Where following parameters are passed

  • Collection – input payload which should be of type collection
  • Counter variable – counter which will hold the value of current loop
  • Batch Size – we can break the payload in our desired size in case we want to process more than 1 record in each loop
  • Root Message – will hold the actual input payload

In our case – payload.sale will hold the collection

Add logger to print each payload

Run the application to see the results

First logger – will print the actual input payload

INFO  2021-12-29 12:10:12,090 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>
INFO  2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
  <shipping>23231</shipping>
  <desc>ok</desc>
  <price>12</price>
  <quantity>12</quantity>
</item>

For Each Logger (First iteration)

INFO  2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
  <shipping>23231</shipping>
  <desc>ok</desc>
  <price>12</price>
  <quantity>12</quantity>

For Each Logger (Second iteration)

INFO  2021-12-29 12:10:12,111 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="3202" saleId="11111">
  <shipping>11111</shipping>
  <desc>ok</desc>
  <price>08</price>
  <quantity>08</quantity>
</item> 

For Each Logger (Third iteration)

INFO  2021-12-29 12:10:12,120 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="5635" saleId="99999">
  <shipping>99999</shipping>
  <desc>ok</desc>
  <price>333</price>
  <quantity>33</quantity>
</item>

Use the Transform message in For Each Scope to convert XML to json into the Anypoint Studio Console.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *