“ForEach” service is used to loop over data retrieved by another service and access the data sequentially. This is EASYProcess’s way to create loops. The ForEach service is meant to be used in combination with the EndForEach service. It is given a SelectorValue (Ex: “WorkData/Inquire”) to reference and allows you to iterate through the results. It does this by automatically indexing the path specified in the SelectorValue input of the ForEach loop.
For example, if a SelectorValue was given as “WorkData/ListOfAddresses” and the ForEach loop were to run for the results of the ListOfAddresses, it would automaticaly index starting at 0.
- So on the first iteration, WorkData/ListofAddresses/ABAN8 will become WorkData/ListofAddresses/ABAN8 which will access the first node of the selector path.
- On the second iteration, WorkData/ListofAddresses/ABAN8 will become WorkData/ListofAddresses/ABAN8 and so on...
Every service you place between the ForEach and EndForEach will run for every iteration of the SelectorValue.
“ForEach” service should be named same as the “EndForEach” service with except without the prefix “End”. This is how the two services are tied together and distinguish themselves from other sets of ForEach servies on the process canvas. Example: ForEachName should have a corresponding EndForEachName.
If you wish to end the ForEach loop early, another service exists called ExitForEach that ends the looping.
Note: Since EASYProcess uses XSLT predicates to filter the original selector node, the selector node itself cannot contain XSLT predicates.
ForEach Use Examples
Basic Use Example
In this process, we are using “ForEach” service to loop over the data retrieved by an Inquire service, “GetCartDetails”. In the loop we’re performing a comparison using “BinaryDecision” service to check whether the cart has reached its limit for items. If it has not, we price the item and then update the cart with the prices retrieved from the pricing process. After this, we end the ForEach
Here is closer look at how the service is configured.
The input “SelectorValue” should be set to the service name we would like to loop on.
Accessing Data Within the loop
Since we are looping on “GetCartDetails” service, If we were to access any output returned by that service, indexing is handled by EASYProcess. When we reference it, we can still sued WorkData/GetCartDetails/BranchPlant, but EASYProcess will automatically convert it to WorkData/GetCartDetails[Index]/BranchPlant.
Creating a ForEach Loop
There are two ways to create a ForEach loop.
Dragging Services Onto the Process Canvas
The first way is to drag “ForEach” and “EndForEach” from process flow elements. Once the services are on the process canvas, the “ForEach” service can be configured by opening the service and entering value in the SelectorValue parameter. Since dragging the services onto the canvas, gives them the default service name, you will need be aware of the naming convention if you plan on having multiple ForEach loops on the canvas. The “EndForEach” service should be named same as the “ForEach” service with “End” as a prefix. Example: ForEachName should have a corresponding EndForEachName.
Right-Clicking a Service
The second way to create a “ForEach” loop is to right-click on the service we would like to create loop on and select “Create ForEach Loop”. That will create both “ForEach” and “EndForEach” services with the correct naming convention and the input SelectorValue already configured.
Maintaining a Sum in a Loop
Sometimes as you For-Each through results you would like to maintain a running sum. This can be achieved through an EASYProcess ForEach loop and “Sum” Evaluate which is set override itself (NewSum=OldSum+[value]).
In this example we have a table which has user input textboxes. The table has three columns. The product code, a column which denotes if order is placed or not, and a user input for the product cost. Our objective here is to return a total sum of all the the products that have been ordered and display it on the page as “Grand Total”. To achieve this we first run a split over the desired columns. In this case the columns “Order Placed” and “Product Cost” are relevant to us.
Now we set up a For-Each loop that will iterate through the results of the Split service. Before the For-Each loop begins, we set a variable “Sum” as 0. This will be our running count which will grow as we continue the loop.
Inside the Split’s for-each loop, we can first check if the “OrderPlaced” flag is true. When it is equal to “True”, we add its value to the running sum with the “Sum” Evaluate service. Finally when the loop is finished, we have our final total in the “Sum” Evaluate service.
In the SetFieldProperties, we set the “Grand Total” text box with the sum variable by referencing the “Sum” evaluate.
Here we can see that we have two evaluate services named “Sum”.
- The outer “Sum” contains an eval named “Total” that has been initialized to 0.
- The inner “Sum” also contains an eval named “Total”. Here the value of total is set to the effect: (Sum/Total + Split/ProductCost)
This way the inner “Sum” will start off with 0 as the seed value and keep updating itself with the calculated total of the incoming ProductCosts also while making sure their OrderPlaced flag is true. In our example Sum/Total will increment as follows:
- Sum/Total = 0 + 12.49 = 12.49
- Sum/Total = 12.49 + 14.99 = 27.48
- Sum/Total = 27.48 + 9.99 = 37.47
IMPORTANT: For a service to work like this, the OverrideExistingOutput property for the service needs to be set to True.
Below are screenshots of how the sum increments while the For-Each loop runs the rows one after the other.