Designer > Implementing a BPMN Gateway or Control Flow > For Each

For Each

BPMN Implementation: Multi-Instance control flow
The For Each activity contains a scope activity and executes it for a specified count. The execution iterations can occur in parallel or in sequence. The number of iterations to execute is determined by evaluating expressions for a start and final value. These values are inclusive, so a start of one and a final of 10 causes the enclosed scope to execute 10 times.
The expressions for the start and final values must evaluate to an xs:unsignedint. If it doesn't, a runtime exception of bpel:forEachCounterError occurs. If the final value is greater than the start value then the for each will not execute.
The counter name of the For Each is manifested as an implicit variable within the child scope. The initial value of the variable is set to the current value of the For Each's iteration, which ranges from the start value to the final value. This variable is accessible to all activities nested within the For Each's scope and appears as a variable within the child scope.

Using a Completion Condition for Processing N of M Branches

A For Each activity can include an optional Completion Condition Expression property that can in turn include an optional Count Successful Branches Only property.
The completion condition expression can be used to process at least N out of M branches. The expression evaluates to an xs:unsignedint value that is used to define condition of N out of M. It is evaluated once, when the for each begins its execution. If the completionCondition is not an xs:unsignedint, or it is determined that it cannot be met prior to executing, the For Each faults with a bpel:invalidBranchCondition. An example of this would be a completionCondition value of five when there are only three iterations of the for each.
The property Count Successful Branches Only can be set to Yes or No. If set to Yes, it means the Process Developer engine counts branches towards it completion condition that have completed normally (meaning the child scope of the for each iteration did not catch a fault and is eligible for compensation). The default value of No results in the For Each counting the completion of any child iteration towards its completion condition.
At the completion of each iteration, the For Each tests the number of completed scopes versus its completion condition (taking into account the Count Successful Branches Only setting as described above). If it there are not enough iterations remaining to fulfill its completion condition, the For Each faults with a bpel:completionConditionFailure and any remaining iterations do not execute, or are terminated in the case of the parallel for each.
A common use of the completion condition is to process only the minimum number of items necessary to complete a scope of work. For example, a BPEL process can call out to many partners in parallel, asking for approvals on an item. The process can require that only three approvals be needed to continue processing the item. Even though the process requested many approvals, it can break off the approval scope of work when only three approvals are received. It can then continue with further processing.

Parallel For Each

You can create a parallel or sequential For Each. In a parallel For Each, instances of the enclosed activity occur simultaneously. Each counter variable is initialized to one of the integer values in the count.
Use a parallel For Each for simultaneous processing of document parts. For example, you can process parts of a complex message variable, such as line items in a purchase order.
A default Message Exchange property that keeps together the correct pair of receive/replies or onMessage/replies, is automatically and implicitly declared in the scope. For details, see Message Exchange Declaration.

Sequential For Each

In a sequential For Each, instances of the enclosed activity execute one at a time.
Required Properties
Optional Properties
Counter Name
Name. See Selecting Activity Labels
Start Counter Value
Completion Condition Expression
Final Counter Value
Count Successful Branches Only
Parallel Execution Flag
Join Condition. See Creating a Join Condition for an Incoming Link
Suppress Join Failure. See Process Properties
Comment. See Adding Comments to a Process
Documentation. See Adding Documentation to a Process
See Setting Visual Properties and Using Your Own Library of Images
Execution State. See Viewing the Execution State of an Activity or Link
Extension Attributes and Extension Elements. See Declaring Extension Elements and Attributes.

To build a For Each

  1. 1. From the Control Flow palette, drag a Multi-Instance activity to the Process Editor canvas.
  2. 2. In the Properties view of the For Each, fill in the required properties:
  3. 3. Fill in the properties for the Scope container.
  4. 4. Specify all the properties for each activity in the scope container. Be sure to set a message exchange property for matching receive/replies and onMessage/replies within the scope if parallel execution is enabled.

XML Syntax

<forEach counterName="NCName" parallel="yes|no">
<startCounterValue expressionLanguage="anyURI">
<finalCounterValue expressionLanguage="anyURI">
<completionCondition extension-attribute
<branches expressionLanguage="URI"?
Example 1: 100 Executions, Executing if 10 Succeed
<forEach counterName="counter" parallel="no">
<branches countSuccessfulBranchesOnly="yes">
Example 2: Serial For Each with Completion Condition
Example 3: Execution Thread of a Parallel For Each Activity
The following illustration shows the execution thread of a parallel For Each that has a counter value of 10. Notice that 10 instances of the enclosed scope are running in parallel.

Faults Associated with a For Each Activity

The following faults can occur with a For Each activity: