Achieving Nested for Loop using Unbound Rules

Let me explain you through a scenario where you have smart form with 2 List views Brands List & Products List and a button to save the data as shown below.

1 Form


Now there is requirement here that for each brand there should be at least one product available before you perform save. Now to achieve this validation requirement in general approach we do something like below

foreach brand in brandsList
  foreach product in productsList
     //check if a product exists for that brand
      If (Found)
          //exit loop and go to next brand
         //Fire Validation and exit loop

Now to achieve the same thing in smart form rules is not possible directly as K2 does not allow you to add for loop with in for loop, but we have a workaround which is using unbound rules. Let’s see how we do it.

We will be requiring 2 hidden data labels hdnDLIsValid which contains default value as False and hdnDLBrandName and 2 unbound rules ValidateLists and LoopProductBrands

 Now add the rules in LoopProductBrands as shown below.

Loop Product Brands


Similarly add rules in ValidateLists as shown below



ValidateLists acts as a parent for loop which loops through all brands list and LoopProductBrands acts as a child for loop which loops through all products.

For each Brand we are storing the brand name in hidden data label hdnDLBrandName and then we are calling loopProductBrands, in which we are comparing the value in hdnDLBrandName with the column value in second List which is Products List.

If the value matches then it will the set the value of hdnDLIsValid to True.

Once the products loop gets completed, it checks whether hdnDLIsValid is still false, if yes then it means we have not found a match, so validation is failed and we can show the validation message and stop rules execution further.

Else if hdnDLIsValid is true then we found a match for that brand, so we can reset the value of hdnDLIsValid to its default value and move on to the next brand.

Now in the save Button Click we just have to call the unbound Rule ValidateLists before the code for saving the data.



Now when you execute it should show you messages as in below screen grabs


Here a product is missing for brand LG, so the validation message gets fired for that particular brand.

Error Message


Here the validation gets passed as all brands has at least one product

Success Message

Finally this is how we can make use of unbound rules to achieve some complex validations like this.


Author: Vijay

I'm a K2 Certified Developer, helping teams to automate, design, develop & maintain business process applications connecting across multiple platforms with multiple technologies.

One thought on “Achieving Nested for Loop using Unbound Rules”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s