Sample Guide - ForEach

Objective

Explains the functionality of <forEach/> construct, where, user can implement repetitive tasks. The tasks can be sequential or parallel based on parallel property. So <forEach/> becomes advantageous comparing to <while/> or <repeatUntil/>, when the repetitive tasks are parallelized.


Prerequisites

  • Log in into BPS server admin console.
  • Under Business Processes -> Add BPEL.
  • Upload the TestForEach.zip , (all samples are located at our sample repository.)
  • Under the Business Processes -> Processes.
  • Under the WSDL details widget -> Create instance

Overall Idea

<forEach  parallel="no" counterName="ForEachCounter">
    <startCounterValue>"1"</startCounterValue>
    <finalCounterValue>"3"</finalCounterValue>
    <scope bpmn:label="ForEach">
        <sequence>
            <assign>
                <copy>
                    <from>concat($thisStartRequestMsg.body/text(), $ForEachCounter)</from>
                    <to>$thisStartRequestMsg.body</to>
                </copy>
            </assign>
        </sequence>
    </scope>
</forEach>

In this sample, 3 iterations take place based on the values of <startCounterValue/> and <finalCounterValue/>.

The property counterName can be used as a local variable inside the child scope to store current iteration value. In the above example, ForEachCounter variable value can be 1,2 or 3.

Then inside <scope/>, repetitive task can be implemented.


Note : <forEach/> can have only one <scope/> child.