The Loopy Loops of InsightConnect

image What is this thing?!

Here’s the official definition:

A loop is a series of instructions that is repeated until an exit condition is met. Loop Steps allow you to execute a sequence of other steps within the loop, until the exit condition is satisfied. You can make the sequence within the loop as long or as short as you need.

That’s a bit of a mouthfull! Loops are really just a series of steps that are repeated according to some rule.

There are two types of loops in InsightConnect. They’re accessed with the same icon - you can then change the type:

  • Repeat Over
  • Repeat Until

Both types of loops behave the same. The big difference is in how they determine how many times the loop runs. A Repeat Over loop runs once for each element in an array - or in english once for each item in a list. In this way, the number of times the loop will run varies depending on the input. Repeat Until, on the other hand, runs a set number of times.

Repeat over is like placing silverware next to each plate already put on the table. You start at the first place setting and put down a fork, a knife, and a spoon. Then you go onto the next plate. You continue until you’re out of plates. This same “loop” will work to set a table for 1 person or to set the table for 50.

Repeat until is like setting the table for 10 people. You walk up to the table and put down a plate, fork, knife, and spoon then move onto the next spot and repeat until you’ve done it 10 times.

There’s a subtle difference between the two types of loops - but it’s important. Repeat Over has a current item - in this example the current plate you’re working on. We can reference this “current item” in our loop - it’s called $item - and this special reference changes to the next item for each iteration in the loop.

Going Straight

The next element of a loop is a “break” step. This step lets you prematurely terminate the loop before you’ve met the loops initial condition. To use our table setting example again, you could have a stack of plates and the loop could be defined as “loop over this stack of plates”. Your loop instructions are “put these plates on the table one at a time until you get to a blue plate - then stop”. This “until x” is a break step. It “escapes” from the loop and causes the workflow to “go straight” instead of looping back to the beginning of the steps in the loop.

Loop Results

Loops can be extremely useful for repeating the same set of tasks over, and over, and over again. However, the results of a loop are usually NOT something you want to iterate over. Let’s abuse our table setting analogy again. You’re now the manager and you want to find out if the table has been set. You don’t want to ask “so, did you set the first place setting” then “did you set the second place setting?” etc. You just want to know if the entire loop was completed successfully. You want to ask “did all the place settings get set” and maybe “do all the forks match”.

In InsightConnect we handle this with Loop Outputs. A loop output is just a way of addressing the loop as a single task instead of multiple tasks repeated multiple times. This allows future steps in your workflow to not even know the loop exists - they see it as a single step with the outputs you define.

A Loop Example

Loops aren’t really complicated. Now that we’ve covered the theory, let’s see a loop in action!

For this example I’m going to abandon my dinner table - InsightConnect just doesn’t have a suitable analogue for a plate. After all, it’s a SOAR product - not a food delivery service.

Let’s take a list of email addresses:

Our Mission (which we choose to accept) is to:

  • compile a list of domains
  • count how many example.com addresses there are
  • determine if any of them are “contoso.com” addresses.

So, for the sake of our example, our workflow is going to start with an API trigger with a single array output called “addresses”:
image

Once our trigger is created, we’re going to add a “loop” step immediately under our trigger. We’re going to Repeat Over the array we defined in our trigger:
image image

Inside our loop we’ll create a pattern match step to separate the name from the domain portion of our addresses (this’ll help with the regex):
image image
Note our use of the $item attribute - this means for each iteration of this loop we’re going to process the current item in our pattern match step.

Loop outputs are a property of the loop, so we need to go into Configure Loop to set them up.
image
Here are the types of loop outputs we have available:
image

What we’re needing from this loop is:

  • an “array” of domains
  • a “count” of “example.com” addresses
  • a “boolean” that’s true if any of the addresses are in the “contoso.com” domain

We’ll start with our “array” loop output. This is straight forward - we’re just going to include the domain portion of our address in the array output.
image

Next we’ll create a second loop output that counts our instances of “example.com” domains. For this one we’re going to use the conditional to only do a “+1” to our count if our domain is “example.com
image

Finally we’re going to add a “boolean” loop output that will be true if any iteration of our loop has a domain that equals “contoso.com
image

Finally, back outside our loop, we’ll add an artifact card that references our loop-output
image

Here’s our completed workflow (main flow on the left, loop on the right):
image image

And here’s our results:
image

I hope this helps explain what loops are and how you can use loop outputs. Please ask questions and share your experiences!

EXTRA CREDIT

Let’s say we wanted to deduplicate our list of domains. This is a fairly common need - we want a “UNIQUE” list of domains instead of having “example.com” listed a bunch of times. This is easy to do, but it’s not part of the “loops” in InsightConnect.

In our workflow, add an Array Match step (from the type converter plugin) under our loop and specify our “Domains” variable from our loop for both Array1 and Array2 and make sure Deduplicate is set to true. With these settings, the Array Match step just deduplicated the array.
image

Finally, we’ll update our “Results” artifact to be a little smarter - we’ll use handlebars {{#if}} helper to make our card a little easier to read:
image

Which gives us this final result:
image

2 Likes