Friday, February 16, 2018

A simple example of the for-each functionality

A component that I frequently use in my BPEL processes is the for-each component. It's an easy feature to loop through data, assert data to a variable and do multiple service calls. I will show you a simple example of how the for-each component can be used. Suppose we retrieve a list of folder ID's and for each of these we will do a service call which returns additional data about those folders. This data will be asserted altogether so we can do some data transformation later in our BPEL process.

So this is the list of folders we are working with and is stored in a variable. We want to call a service that retrieves additional data about the FOLDERGUIDs.
 
The for-each part of our BPEL process looks like this:

After dragging the for-each component to our canvas we can edit the conditions. We can give our for-each loop a name. In this case I just called it 'ForEach'. The for-each has a counter variable that keeps track of the index of loops. Here I named the counter variable 'ForEachCounter'.
Next we define the start and final value of our counter. Here the counter starts with index 1. The final value of the counter is determined by the number of FOLDERGUIDs I have in my list. That's why I used the count function that will calculate the occurences of FOLDERGUIDS in the variable that holds the list.
In the tab 'Completion' you could enter an expression that will determine when to exit the for-each loop. For example when a certain condition is met it would not be necessary anymore to proceed with the loop.
Next we want to provide the element FOLDERGUID from our list to be the input parameter to call the service that retrieves additional data about our folders. When we assign the FOLDERGUID as the input parameter we have to keep the counter in mind that keeps track of the position of our FOLDERGUIDS. That's why we put [$ForEachCounter] in our expression.
The data returned could be asserted to a new variable for further transformation.

And that's it basically! Here I have demonstrated a common use of the for-each function in BPEL.