Loop Functions

Venkat R
Venkat R
  • Updated

This article introduces the Loop function and show how to build Pipelines with loops using this function.

Building with Loops

Loop function can be found in Function List in both Entity and Field Pipelines.

Loop in Function List.png

You can drag the function to the Canvas and start configuring the function. Notice below that once you drag the Loop function few additional nodes have been automatically created and connected in the Canvas. All the nodes that need to be executed within the loop are placed between For Each node and End Loop node.

Loops Unconfigured.png

Configuring Loop function

Loop Configuration 1.png

There are two options available while configuring Loop - Loop Index and Loop Variable.

Loop Index

When Loop Index is chosen, you would provide Start Index and End Index. Both these values need to be numbers and can be tokens which will resolve to numbers. All the functions inside the loop would be executed from start index to the end index. In the example above loop would be executed 5 times. Each iteration of the loop would expose the index as token - {{currentLoop.index}}. The value of this token would range from 1 to 5.

Loop Variable

Loop Configuration Variable.png

When Loop Variable option is chosen, then you would need to provide a List or Map over which to iterate. Typically, this would be a token which can either resolve to a List or a Map.

  • List - If the value is List, then the loop would be iterated once for each element of the list. List element is available using the token {{currentLoop.value}}.
  • Map - If the resolved value is a Map, then the loop would be iterated once for each entry in the Map. Key and Value in Map entry would be available as tokens {{currentLoop.key}} and {{currentLoop.value}} in the loop iteration.

Using Loop in a Pipeline

Below is a simple Pipeline with a Loop function. We will use this an example to go over different parts of Loop structure.

Simple Loop Pipeline.png

In this pipeline, the Loop function iterates over the list in the temporary variable - loop_variable. In each iteration, flow goes through the Decision node and if decision evaluates to true, then the current element in the list is added to the list loop_list in "Add to List" function. Below is the configuration of the Decision node and Add to List node.

Decision in Loop.png

Add to List in Loop.png

Once the pipeline has iterated over all the elements in the list, flow goes through After node and the Find Value function evaluates the temporary variable loop_list token and result is stored in the Syncari field. 

Loop Nodes

We noticed above that when Loop function is dragged on to the Canvas, there are three additional nodes that are created. We will discuss them briefly here.

  • For Each - This node marks the start of the loop iteration. Nodes after this node would be executed in the the Loop.
  • End Loop - This node marks the end of the loop iteration. All the different paths from the For Each node would terminate in the End Loop node.
  • After - This is the first node after the Loop. This node serves as visual cue to indicate continuation of the pipeline after the loop.

Loop Input and Output

  • Output of the Loop function is same as Input to the Loop function. This means any transformations done inside the loop do not affect the output of the loop.
  • As a follow up to the first point, input to the Loop function is  sent as input to the first node of the loop (node after "For Each" node) in each iteration of the loop and any transformations to this input are not carried over to the subsequent iterations of the loop.

Nested Loops

Loop function can be nested inside another Loop function provide nested loop functionality. Below is an example of a Pipeline with nested loop.

Nested Loop 1.png

Outer Loop and Inner Loop are loop functions iterating over two different lists. In the Add to List function we concatenate the current element in the outer loop and inner loop and resulting value is adding to the result_list. Note here that current loop element in the list can also be accessed using function label using the token of the form {{<Loop Label> Value}}.

Note above that After node at end of the inner loop connects to the End Loop of the outer loop. In general After node at the end of inner loop would indicate continuation of the outer loop. 

Tokens

We will enumerate all the tokens that are available with the Loop function.

  • {{currentLoop.index}} - Index of the current (innermost in case of nested) loop. This is applicable only if option selected is Loop Index.
  • {{<Loop Label> Index}} - Index of loop function labeled Loop Label. This token can be used to refer to the Index in any level of nested loop. This can also be used to refer to the last index of the Loop outside the loop.
  • {{currentLoop.value}} - Value of the list element in the current loop. This is applicable only if option selected is Loop Variable.
  • {{<Loop Label> Value}} - Value of the current list element in the loop labeled Loop Label. This token can be used to refer to the list element in any level of the nested loop. This can also be used to refer to the last element of the iterated list outside the loop.
  • {{currentLoop.key}} - If the Loop Variable configured is a token which resolves to a Map, then two tokens are available. {{currentLoop.key}} refers to the key of the current Map entry and {{currentLoop.value}} refers to the value of the Map entry.
  • {{<Loop Label> Key}} - Key of the current Map entry in the Loop labeled Loop Label.

Legacy Loops

Loops can be implemented in Syncari without using Loop functions, but we are deprecating this functionality and it is not available for new pipelines. In this section, we will look at an example of a pipeline with legacy loops and see how this can be rebuilt using Loop functions.

Below pipeline uses legacy loops to iterate over a multivalued field products and converts each value to upper case and resulting multivalued list is stored in Syncari.

Screenshot 2024-09-25 at 12.13.38 PM.png

Implementation of the same pipeline with Loop functions is shown below.

Screenshot 2024-09-25 at 12.27.40 PM.png

Let's highlight a few things going on here.

  • Loop function iterates over the products field.
  • Find Value function evaluates the current loop value using token {{currentLoop.value}}.
  • Result of Upper function is stored in temporary variable result using Add to List.
  • Temporary variable result is evaluated and stored in Syncari.

You'll notice that while the pipeline using the Loop functions has more nodes, the explicit loops are easier to follow and understand.

If you create a new Pipeline with old legacy loops, then you would see an error while testing with Live Tests.Screenshot 2024-09-25 at 12.45.17 PM.png

Share this

Was this article helpful?

0 out of 0 found this helpful