Using the Loop Feature in an Activity

When creating a flow, you may want to iterate a certain piece of logic multiple times. For example, you may want to send an email to multiple people based on the output of a certain activity (let's call it activity1) in your flow. You can do this by adding a SendMail activity following activity1 in your flow and configure the SendMail activity to iterate multiple times when activity1 outputs the desired result. Each iteration of the SendMail activity is used to send an email to one recipient. This saves you the effort of creating multiple SendMail activities.

Keep the following in mind when using the Loop feature:
  • Iteration is supported for an activity only. You configure the iteration details in the Loop tab of the activity.
  • There are certain activities that inherently do not require iteration, for example the Return activity, whose purpose is to exit the flow execution and return data to the trigger. The Loop tab is not available in such activities.
  • You cannot iterate through a trigger.
  • For apps that were created in Project Flogo™ and imported into TIBCO Cloud Integration - Flogo (PAYG), the key type in the Loop tab is converted from string to the relevant data type of value in TIBCO Cloud Integration - Flogo (PAYG).

To configure multiple iterations of an activity, do the following:

Procedure

  1. Click the activity in the flow to expose its configuration tabs.
  2. Click the Loop tab.
  3. Select a type of iteration from the Type menu.
    The default type is None, which means the activity will not iterate.

    Iterate

    This type allows you to enter a number that represents the number of time you would like the activity to iterate without taking into account any condition for iterating.

    Click iterator to open the mapper to its right. You can either enter a number (integer) to specify the number of times the activity must iterate or you can set an expression for the loop by either entering the expression manually or mapping the output from the preceding activities or trigger. You can also use available functions along with the output from previous activities and/or manually entered values to form the loop expression. The loop expression determines the number of times the activity iterates.
    Important: The loop expression must either return a number or an array. The array can be of any data type. If your loop expression returns a number, for example 3, your activity will iterate three times. If your loop expression returns an array, the activity will iterate as many times as the length of the array. You can hover over the expression after entering the expression to make sure that the expression is valid. If the expression is not valid, you will see a validation error pop up.

    If you select this type, the Input tab of the activity displays the $iteration scope in the output area of the mapper. $iteration contains three properties, key, index, and value. index is used to hold the index of the current iteration and value holds the value that exists at the index location of the current iteration if the loop expression evaluates to an array. If the loop expression evaluates to an array of objects, value also displays the schema of the object. If the loop expression evaluates to a number, the value will contain the same integer as the index for each iteration. To examine the result of each iteration of the activity, you can map index and value to the message input property in the LogMessage activity and print them. key is used to hold the element name when configuring a condition if the value evaluates to an object. However, you can map only to the output of the last iteration if you did not set the Accumulate Output checkbox to Yes. See Accumulating the Activity Output for All Iterations section for more details on this.

    Repeat while true

    Refer to https://github.com/TIBCOSoftware/tci-flogo/tree/master/samples/app-dev/loops.sample for an example of how to use this feature.

    Select this type if you want to set up a condition for the iteration. This acts like the do-while loop where the first iteration is done without checking the condition and the subsequent iterations exit the loop or continue after checking the condition. You set the condition under which you want the activity to iterate by setting the condition element. The condition gets evaluated before the next iteration of the activity. The activity iterates only if the condition evaluates to true. It stops iterating once the condition evaluates to false. Click condition, and manually enter an expression for the condition. For example, $iteration[index] > 5.

    Keep in mind that even though the index for the Repeat while true iteration begins at zero, it does not iterate n+1 times. If you enter 4 as the iterator value then it will execute as the following iterations: 0,1,2,3.

    By default, the results of only the final iteration are saved and available. All previous iteration results are ignored. If you would like the results of all iterations to be stored and available, set Accumulate to Yes.

    You have the option to set a time interval (in ms) between each iteration, which can help you manage the throughput for your machine. To spread the iterations out, set the Delay element. Default delay time is 0 ms, which stands for no delay.

Result

After you enter the loop expression, the loop icon appears on the top right corner of the activity leaf as follows: