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
      }
      else
      {
         //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

 

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.

ButtonSave

 

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 comment

Bagi2info.com

Tips dan Trik Komputer, Tutorial Pemrograman, SQL, PHP, React Native - Expo

Mohamed Ashiq Faleel

Its time to think about Microsoft 365

Common Man Tips for Power Platform, Dynamics CRM,Azure

Venkata Subbarao Polisetty - Microsoft MVP,C# Corner MVP

James K2 Blog

Tips and Tricks about K2 workflow software

the BPM freak !!

"The illiterate of the 21st century will not be those who cannot read and write, but those who cannot learn, unlearn, and relearn" – Alvin Toffle

Start Rule

Learning K2 blackpearl and K2 Five

K2Ranger

Do K2 like a BOSS!!

Maverick

discover your own way and lead the world... with Divya Raj

jeylabs

Work Intelligently

Chasing Time - Mobile Gaming Photography Tips Tricks

#iamtheiosphotographer Pro Mobile Game Reviews, Tips, Tri

Jozsef Torsan

Founder & Software Engineer - Creator of the Bookmark Ninja Online Bookmark Manager

GreenEggs on K2

Saving the world with K2