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 want to send an email about an output of a certain activity activity1 in your flow to multiple recipients. To do so, you can add a SendMail activity following activity1 in your flow. Then configuring 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.

Keep the following in mind when using the Loop feature:

  • Iteration is supported for an activity only. You configure the iteration details on the Loop tab of the activity.
  • The Loop tab is unavailable for certain activities that do not require iteration. For example, the Return activity. Its purpose is to exit the flow execution and return data to the trigger.
  • You cannot iterate through a trigger.
  • For apps that were created in Project Flogo and imported into Flogo Enterprise, the key type on the Loop tab is converted from the string to the relevant data type of value in Flogo Enterprise.

To configure multiple iterations of an Activity:

    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 does not iterate.

    Iterate

    This type allows you to enter a number that represents the number of times you would like the Activity to iterate without considering 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 triggers. You can also use the 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.

    Warning: 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 iterates three times. If your loop expression returns an array, the activity iterates 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, a validation error is displayed.

    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. The 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 an array of objects, value also displays the schema of the object. If the loop expression evaluates to a number, the value contains the same integer as the index for each iteration. To examine the result of each iteration of the Activity, you can map the index and value to the message input property in the LogMessage Activity and print them. The 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 the Accumulating the Activity Output for All Iterations section for more details on this.

    Repeat while true

    Refer to Flow Design Concepts 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 run 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 the index for the Repeat while true iteration begins at zero and iterates n+1 times. If you enter 4 as the iterator value, it runs as the following iterations: 0,1,2,3,4.

    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 of your machine. To spread the iterations out, set the Delay element. The default delay time is 0 ms, which results in no delay.

Result

After you enter the loop expression, the loop icon appears on the top-right corner of the activity.