Select Page

Patterns and Practices: Iterating Collections

by | Jun 20, 2016

While struggling with While Loops…I was Saved by Execute Member Rule Set

I have a little bit of experience with programming – enough to be dangerous. However, this experience biased me just enough that when presented with the problem below, my first approach was to use a While Loop.

I want to take you through the problem, how I solved it with a While Loop, and why it was much easier to do that same thing with an Execute Member Rule Set.

Problem

Claims have transactions that have been approved (“Transactions”), and pending transactions that need to go through a series of validation rules (“Pending Transactions”). Validation rules that fail create a record in the Denials table with details about why that transaction failed validation. If no Denials were created, the Pending Transaction is considered “Approved” and should be inserted into the Transactions collection. The Pending Transaction should then be marked as “Completed” so that it does not get processed again.


For this example, we’re going to focus on the processing of the Pending Transactions after they have gone through the validation rules.

  • For each unprocessed Pending Transaction that does not have any Denials, I need to:
    • Set the Pending Transaction’s ApprovalStatus to “Approved”
    • Insert the Pending Transaction record into the Transactions collection
    • Set the Pending Transaction’s ProcessingStatus to “Completed”
  • For each unprocessed Pending Transaction that has Denials, I need to:
    • Set the Pending Transaction’s ProcessingStatus to “Completed”

So, naturally, I need to loop through each unprocessed Pending Transaction.

I’ll Use a While Loop!

Ok! On the Claim rules, Add > While!


Oh wait, before I do that, I need a counter. So, above the While Loop, Add > Actions > Declare Variable.

I need an Integer with a Default Value of 1. Easy enough!

Back to my While Loop. Ok, while the counter is less than or equal to the Count of Pending Transactions where the Processing Status Completed….


Hold on, that won’t work. That will just impact the number of times I loop through the Pending Transactions collection, it won’t filter the list of collection members I’m actually looping through.

So, I’ll loop through all of the members of the Pending Transactions collection…


…and then add an If statement within the While Loop.


Ugh. Because I’m at the Claim level, every time I refer to a Pending Transaction, I have to refer to the collection member by index using my counter. Annoying!


NOTE: I realized later that instead of using the GetMemberValueByIndex function on each item, I could have created a variable of type Pending Transaction entity and then set that within the while loop to the current member as “GetMemberByIndex(PendingTransactions,Counter)”. Then, I could have just referred to that variable.fieldname rather than continuing to need to pass in the index every time.


Phew. Okay, now I’m ready. Let’s test this.


DOH! Cycle count exceeded. Argh. I forgot to increment my counter.


Wow. That was tough.

Let’s sum up my While Loop experience. I had to:

  • Create and manually increment a counter
  • Refer to the collection members by index
  • Use an If statement within the loop in order to apply a filter to the loop

Let’s Try Execute Member Rule Set

Now, rather than looping through Pending Transactions in the context of the parent Claim, I can create an Explicit Rule Set at the Pending Transaction level.

This is a fundamental shift in thought – to create the rules at the level where the actions are taking place, rather than at the parent level where you would normally setup your loop.

So, Add Rule Set > Pending Transaction > Explicit.


Now, let’s add a language rule.

If there is nothing in the Denials collection for this Pending Transaction, then it’s approved. So, If the number of Denials = 0…

That was easy! No indexes needed. I’m already in the context of a Pending Transaction!


If it’s approved, I want to insert the information from that Pending Transaction into the Transactions collection.

Add action > add collection member.

This was painful before. I needed to refer to each Pending Transaction by index. Not anymore! I’m in the context of my Pending Transaction, so the “Value” is just the name of the field on the Pending Transaction.


Once I’ve added all of the rules for my pending transaction, I can add the Execute Member Rule Set action at my claim level where I want to do this processing.

On the Claim rules, Add > Language Rule > Create Rule > execute member rule set.

I can pick my Rule Set and my Collection. And….I can add a filter right here!

So, my rule to execute the Pending Transaction rule set states clearly it’s only going to iterate through items that have not already been processed.

Let’s test this!


Yay! No errors.

For all of the reasons that the While Loop was difficult, the Execute Member Rule Set was easy.

Let’s review:

  • No need to create or manually increment a Counter!
  • No referring to collection members by index – You’re already in the context of the member you’re looping through!
  • You can apply a filter directly to the Execute Member Rule Set action. It’s easy to see which items are being looped through without having to dig through If statements within the loop.

https://

BLOG POSTS BY TOPIC:

FEATURED ARTICLES:

STAY UP TO DATE

We’d love to share company and product updates with you! Please enter your email address to subscribe to monthly updates from InRule. 

If at any time you want to unsubscribe, you can easily do so by clicking “unsubscribe” at the bottom of every message we send or visiting this page.