Contact Free trial Login

For Each Scope

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.

General considerations about the For Each Scope:

  • By default For Each tries to split the payload. If the payload is a simple Java collection, the For Each scope can split it without any configuration.

  • For Each will not modify the current payload.

  • For non-Java collections, such as XML or JSON, you need to use a DateWeave expression to split data. Use the Collection field for this purpose.

    In the following example, the Collection field in For Each is set to iterate over an array stored in payload.topics:

    For Each Component

The For Each scope stores each item of the collection in payload during each iteration.

You can also split an array into batches to enable quicker processing. Each batch is treated as a separate Mule message. For example, if a collection has 200 elements and you set Batch Size to 50, the For Each scope iteratively processes 4 batches of 50 elements, each as a separate Mule message.

Example XML

This is an example XML based on the For Each scope configuration detailed above:

...
  <foreach doc:name="For Each" collection="#[payload.topics]" batchSize="1" rootMessageVariableName="rootMessage" counterVariableName="counter">
      <file:write ... >
      <!--Any other module that you want to include in the For Each scope -->
  </foreach>
...

Error Handling

If one of the elements in a collection throws an exception, the For Each scope stops processing that collection and invokes the error handler.

Example Projects

There are several example projects in Anypoint Exchange that you can open in Anypoint Studio to learn more about how to use the For Each scope:

  • Authenticating Salesforce using OAuth2

  • Import contacts into Microsoft Dynamics CRM

  • Importing a CSV file into mongoDB

  • Importing an Email Attachment using the IMAP Connector

  • Importing Email Attachments using the POP3 Connector

  • Querying a Database and Attaching Results to an Email

To download and open an example project while you are in Anypoint Studio, click the Exchange icon in the upper-left corner. Then, in the window that opens, log into Anypoint Exchange and search on the name of the project.

XML Reference

For Each scopes open and close with a <foreach> tag. Components that are affected by this scope are defined as child elements of the <foreach> tag.

Configurable Properties

Property Default Description

collection

payload

An expression that returns a Java collection, object array, map, or DOM nodes.

counterVariableName

counter

Name of the property that stores the number of messages over which it iterates.

batchSize

1

Partitions the collection into sub-collections of the specified size. For example, if a collection has 200 elements and you set the batch size to 50, it processes 4 batches of 50 elements each.

rootMessageVariableName

rootMessage

Name of the property that stores the parent message. The parent is the complete, non-split message.