Reference > Rule Languages > Business Action Language > BAL Constructs > for each <object> in <list>

Summary

Specifies that a group of actions must be performed for every element of a collection.

Syntax

for each <object> [called <variable>,] in <list>:
    - <action>;*

Description

This construct enables you to apply one or more actions to all objects in a collection.

The construct should be immediately followed by a colon (:) and a list of one or more actions, each on a separate line, each preceded by a dash. End each action statement with a semi-colon (;).

The implicit variable `this <object>' can be used in each action statement within the for each loop to refer to the current object. If you nest a for each statement inside another, you can use the called construct to define an explicit name for the object in the 'outer' for each loop in order to reference it clearly from within the nested for each loop.

Example

The following rule shows how to apply a discount to all the expensive items in a customer's shopping cart, if the customer is a Gold customer.

definitions
    set `smith' to a customer ;
    set `expensive items' to all items 
        in the items of the shopping cart of smith
            where the price of `each item' is greater than 1000;
if
    the category of smith is Gold
then
    for each item in `expensive items':
        - apply 5% discount to this item;

Related Concepts

Rule Actions

Related Tasks

Creating Business Rules

Related Reference

else
then
called <variable>

Related Samples and Tutorials

Tutorial: Creating Business Rules