Loop over Repeating Elements with a For-Each Action

The for-each action enables you to loop over a repeating element and process one or more actions within the scope of the for-each action. The number of loop iterations is based on a user-selected repeating element. For example, you may have an integration in which you download a number of files and want to loop over the output of the files. The for-each action enables you to perform this task.

Creating a For-Each Action

Note:

  • You cannot use an unqualified repeating element in a for-each action.
  • When you configure a stop action inside a for-each action, the entire integration is terminated when the for-each action is processed for the first time. The for-each action is not allowed to process more than once. The stop action does not display any message describing this behavior during design time.
  1. Add a for-each action to an integration in either of the following ways:

    • On the right side of the canvas, click Actions Integration actions icon and drag the For Each action to the appropriate location.
    • Click Add icon at the location where you want to add the for-each action, then select For Each.

    The Configure For Each panel opens. This includes a Sources tree of elements.

  2. Click Edit Edit icon under Configure For Each to enter a name and optional description for the for-each action.

  3. Expand the Sources tree to select an element.

  4. Drag a repeatable element to the Repeating Element field. This is the element over which to loop.

    Note:

    Note the following restrictions:
    • The selected element must be repetitive. You can identify repetitive elements by the three-box icon to the left of the element name.
      The process element is expanded to show the repeating element.

    • Any parent of the selected element must not be repetitive.

    • The data type of the selected element must be scalar.

    • Global and nonglobal repeated elements can be selected.

    • If you have a repeating element within another repeating element (that is, a list within a list), you must first create a for-each action and loop over the parent list. This gives you access to the child list during every iteration. You can then create a second for-each action within the scope of the first for-each action and loop over the child list.

    For this example, the element over which to loop is ICSFile.

    For every iteration of the loop, there is a single reference to the repeating element (ICSFile). A current element name file is required for this action to occur.

  5. Enter an alias for the current file of the iteration in the Current Element Name field.


    The repeatable element under Sources is dragged to the Repeating Element field. Below this the Current Element Name field shows a value of currentFile. Below this the Process items in parallel check box is selected.

    Note:

    If you drag a for-each action into a schedule integration, an additional field called Process items in parallel is visible at the bottom of the dialog.
    Process items in parallel check box

    When selected, iterations of the for-each action are run in parallel. For example, if an integration processes a list of files with a for-each loop created over every file, the processing of the entire file is done in parallel for every file. If a variable is declared outside a for-each loop with Process items in parallel selected and updated within the for-each loop, the last updated value for that variable is available outside of the loop. Also, the degree of parallelism (DOP) is set to 1 to avoid concurrency issues.

  6. Click Save.

    The for-each action is displayed in the canvas. A looping arrow indicates that this action performs repetitive looping.

  7. Click + inside the for-each action to add additional actions. These actions define what happens during each iteration of the loop. For this example, scope and switch actions are defined within the for-each action.


    Description of oic3_for_each2.png follows
    Description of the illustration oic3_for_each2.png

Using Global Variables in For-Each Loops

When global variables are used in for-each loops (for example, for logging purposes), they can lead to the following runtime exception when bad or invalid data is associated with the variable:

FORG0001: invalid value for cast/constructor, Cikey=13695578,
FlowId=10025204, Current Activity Key=13695578-BpAss43-BpSeq8.240-4, Current
Activity Label=assignment_ics_api_internal_variable23,

As a recommendation:

  • Limit the use of global variables in for-each loops.

  • Global variable handling requires database access, which results in an added performance overhead.

  • Local variables can be used for logic specific to a local scope.

Tracking the Status of a For-Each Action During Runtime

You can track the status of the for-each action on the Instances page through the tracking diagram and activity stream for an activated integration.

  1. In the navigation pane, click Observability, then Instances.
  2. Click the business identifier value of the integration to track.

    The integration flow (including any for-each actions) is displayed. Any for-each action failures are identified by red arrows. The activity stream is displayed on the right side.

Related Documentation
The following sections provide examples of using a for-each action in an integration: