How to Iterate On Variable Number of Data

Updated 

In IVR systems, a loop node is a powerful node that allows for efficient and dynamic handling of list or array variables. When dealing with collections of data, such as a list of accounts or transactions, the loop node enables the system to iterate through each element of the array, performing specific actions or operations on each item in a systematic manner.

By utilizing a loop node, IVR systems can automate repetitive tasks and provide personalized interactions with callers. The loop node acts as a control structure, enabling the IVR system to efficiently traverse through the elements of the array, executing pre-defined actions or sequences of operations for each item. For instance, in the context of a banking IVR, the loop node can be utilized to provide callers with a summary of their account balances, transaction history, or other account-related information.

Pre-requisite for configuring this node in IVR:

  1. Sprinklr Voice should be enabled for the environment and your user should have IVR View, Edit or Create permission.

In order to Loop node in between the IVR , In the IVR Builder, click the New Space_Icon_Addition.png  icon on the path where you want to add Loop and select the Add Loop node under system nodes.

Here is the sample configuration for Add Loop node-

Some of the business use cases-

  1. API is returning the list of account and you need to vice out the summary of all account or send summary on SMS/Email

  2. API is returning the list of account and you need to ask the customer to choose the account

    Example - Press 1 for account ending with XXX, Press 2 to account ending with XXXX,... So on

  3. Tag one custom field for all cases coming from fetched multiple record from fetch record node

Components-

Major components are-

  1. Collection Variable: This is where you provide the name of the variable that holds the list or array. The loop node will iterate through each item in the list.

  2. Loop Direction: You can choose the direction in which the items in the list will be processed. For example, if you select "First to last," the iteration will start from the first element of the array.

  3. Loop Variable name: In this you need to define the name of the variable that will hold the value of the current element during each iteration. This variable will be used inside loop to take action on each item in the list.

Loop node will have two paths, it will look like this on the canvas-

In the "Each Item" path, you can add any nodes like "Operator Says", "API Node", etc to perform actions on each element of the list. The value of the current iterated element is stored in the variable specified in the "Loop Variable Name." Once you finish performing actions on an element, you can add it back to the loop node.

After all the elements have been processed, the IVR system will move forward with the "After Last Item" path.

Resources related to loop node-

  1. Loop Variable name- In each iteration this variable will hold the value of the current element on which iteration is happening. This variable will be used inside loop to take action on each item in the list.

  2. Index- The index represents the current count or position of the iteration. It helps you keep track of where you are in the loop. For instance, if you are processing the third element of the list, the index will be 3.

    Note: Index starts from 1.

    If Loop Variable name is current then you can access current and current index inside the loop using resource menu.

Problem Statement- Want to send individual SMS corresponding to every card number. We are getting a list of card number in an array in card_number_list variable.

SMS we want to send-

Hello Sir,

Your Outstanding balance in card number XXX is ABC.

Solution-

We will use loop node to iterate on card_number_list variable and send SMS for each element. In this case, variable on which we will iterate: card_number_list

Configuration of Loop Node-

  1. Collection variable -> card_number_list

  2. Lets name the loop variable -> current_card_number

  3. Lets take the direction -> Top To bottom

In each item path we will add an API node to fetch card summary and use send SMS node and use loop variable to send SMS.

Configuration for this will look somethign like this-

Lets consider for any particular customer the card_number_list is [1234, 5678, 4589, 4523]

1st Iteration-

For this iteration, Loop Variable i.e, current_card_number will be 1234. For the API node this variable will be the input.

This loop variable will be used inside the SMS and API node. We can use this variable to take certain action and when the loop will come back to that loop node the iteration will move forward.

The message configuration for this will be-

Hello Sir,

Your Outstanding balance in card number ${current_card_number} is ${API_output}

The message that will get published to the customer will be-

Hello Sir,

Your Outstanding balance in card number 1234 is XXX

2nd Iteration-

For this iteration, Loop Variable i.e, current_card_number will be 5678.

Same API node and SMS node willl be executed.

The message that will get published will be-

Hello Sir,

Your Outstanding balance in card number 5678 is XXX.

Similarly it will execute for 3rd Iteration and 4th iteration.


How to Break Loop in between?

In certain situations, while iterating through a list using the Loop node, there may be a need to prematurely end the loop for specific conditions before reaching the end of the list. To accomplish this, the IVR system provides a node called the "Break Loop" node.

When you add the Break Loop node to the "Each Item" path of the Loop node and connect it back to the Loop node, it enables you to stop the iteration at a particular element of the list. Once the Break Loop node is triggered, the IVR immediately exits the loop and proceeds to the "After Last Item" leg. This functionality is particularly useful when you encounter a condition or requirement that warrants stopping the iteration process prematurely.

You can add break node in similar way inside the loop. i.e.,

In order to add Break node in between the loop , In the IVR Builder, click the New Space_Icon_Addition.png  icon on the path where you want to add Loop and select the Add Break node under system nodes.

Note: You must add break node inside the loop node only.

Here is the sample configuration for Loop with Break node-

In the example given above, the loop will break when the decision for break loop will satisfy and the flow will move to After last Item.

Note: Always use decision box or some condition before adding break node.