Let's imagine you have some set of data like colors and you want to iterate over all of them. For illustration I will create this data set using Code node:
And after that I will add a Loop node:
After executing a workflow we can see 3 items in Done Branch as expected:
Ok, let's now imagine that we have some another data set like integer numbers and we also want to iterate over all of them in a nested loop. I will create another Code node for this:
And again I will add a nested Loop node:
And finally let's add Set node to a nested loop to show the current values of color and integer number:
The final workflow should look like this:
Now let's execute the workflow and check the results. What we can see is that outside loop's Done Branch has 18 items and the values are quite odd:
This is not we were expecting though. It looks like nested loops are not supported according to the comment of n8n team. But there is a workaround - let's move the body of outside loop to a separate sub-workflow:
It should have "color" input parameter:
And it also requires the change in Set node - we don't have outside loop here but have sub-workflow input instead:
Then get back to the main workflow and add Execute Sub-workflow node to call our sub-workflow with a nested loop:
Pass the color from the outside loop as an input parameter of sub-workflow:
It's time to test it. Let's execute the main workflow and check the results:
This is exactly what we were expecting to see. So for nested loops in n8n it's possible to use sub-workflow workaround.
No comments:
Post a Comment