Use a loop in a loop

Hi there all!
I am trying to create a loop in a loop and just found out this is not ( yet?) possible or I’m doing something wrong :smiling_face_with_tear: .( would not be the first time :slight_smile: )

What I’m trying to accomplish:
I have a registration form where an employee can select multiple employees and multiple dates and the number of working hours. Purpose is in one go to create for example for 3 colleagues 5 days with 8 hours per day logged hours. I use multiple select option so that the employee has the least amount of work logging their team’s working hours.

To save this, I would like for each colleage and for each date to create a record with: hours, date and employee.
With this logic I would like to loop through every selected employee and then loop through each selected date to create the desired record.

Any ideas to solve this?
Thanks in advance!

P.s. Am curious why this is not possible, not to complain but to get a better understanding of Triggre and to find a solution for these challenges more easily in the future.

Greetings Niels

1 Like

Hi @niels.deboer,

Thanks for raising this question, and welcome to the community!

Why there are no loops in loops
First, some background to this, as this is indeed a design decision. The “problem” with loops in loops is that the number of actions performed can quickly get way out of hand. This can potentially lead to performance problems, infinite loops and much higher than expected costs as a result of that.

That being said, there are several ways to work with situations where you could also use loops in loops. Here are, for several situations, solutions.

Solution 1 - multiple steps
In many cases you can subdivide the process into multiple steps. For example, if you have multiple companies, and those each have employees for which you want to perform a specific process you can do the following:

  1. Select the companies you need with a lookup
  2. Select the employees you need, from the companies you looked up, in a second lookup
  3. Use this last list to perform a repeat action

In your case, what I would probably do is store/link the list of dates in the employee data item. For example, something like ‘Temp registered days’. This would be your first repeat action.

Then, with that data stored, you can create a 2nd repeat action over the employees (the list) / Temp registered days’. This will get you an iteration for each ‘Temp registered day’ which is linked to an employee.

The advantage of this solution is that is completely synchronous; meaning all the data is generated before the flow part ends.

Solution 2 - data triggres
The most commonly used solution is to leverage the use of data triggres in automation flows. In general:

  1. Loop through first data item (e.g. selected Employees)
  2. Store list of 2nd items to loop through in each item (e.g. store list of dates in each Employee)
  3. Automation flow with data triggre on this property, triggered for each changed data item (limited to this specific property)
  4. Inside the automation flow, you have a repeat action to loop through the list

In your case, this means for each employee that is selected, you’d store a list of dates (i.e. as a Date list property).

Then you have an automation flow that starts whenever that list of dates property is changed. In that automation flow you would create the hour registration for each date.

Note that you cannot change the list property inside the automation flow, since that would trigger the automation flow again (this is another protection against infinite loops).

I hope that either of these solutions works for you!

2 Likes

Thanks Jesse for the fast and clear reply! I will try some things out that you suggested and will keep you posted on my progress!

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.