Loops in Loops without Subworkflow

As I couldn’t find an answer that Loops in Loops are possible without subworkflow, I wanted to share the solution we came up with to make that possible:

  1. After inside Loops are done, set reset=true via Code Node: $('second loop').params.options.reset = true;
  2. The Loop path of the inside Loop (“second loop”) needs to set reset=false via Code Node: $('second loop').params.options.reset = false;

Here I have an example Workflow to show how it works:

Workflow
3 Likes

Wow, how did you come to this solution?

It changes a lot of things for me.

Manipulating a node state during its execution creates a lot more complexity in n8n. :grinning_face:

Interesting because I’ve never understood the need for the reset toggle.

Are you able to share any more details on your specific use-case? I’ve tried to run it myself with and without the reset calls in the code nodes and can’t quite tell the difference…

The difference is visible when you check the console outputs. I just ran it real quick, the first run (before the “xxxxxxxxxxx”) is with the reset in code nodes.

The second run (after the “xxxxxxxx”) is without the reset.

So the difference is, without the resets, the second loop only runs once. The subitems of the other 2 runs in the example are not looped in the second loop.

1 Like

Thanks for the clarification… $('second loop').params.options.reset seems like it’s read-only for me which explains why I wasn’t seeing the difference. I’m on community edition v1.99.0.

Interesting technique nonetheless! Will give this a study and try to figure out what’s going on.

I do wonder if the nested loop not running for the other sub items should be reported as a bug? Seems like it should? My quick test case: The sub items for parent items 2 & 3 are not marked as seen when the main loop is done.

Edit: Okay, seems like the solution was found by @shrey-42 Nested Loop over item doesn't work properly - #3 by Roman_Garanin

1 Like

If it helps, the second loop reset parameter is enabled in the “off” position at the start.

In my case, I needed to activate the reset option, even though it was in the ‘off’ position.

I don’t know, but maybe it changes something regarding the ability to read/write in the node?

Thank you for the details, @Michael_Nussbaumer. I’m struggling a bit with this loop logic ^^

Where is this log console ?
I only have logs from N8N, and they don’t show this level of detail.

Thx to @Jim_Le, here is a more elegant solution:

  • Instead of resetting the “reset” state, we use an Expression for reset = {{ $('second loop').context.done }}
  • So basically the “second loop” switches automatically to reset after each run.

The Debug output from my screenshot is just the Developer Console of the browser, when running a Workflow manually

Updated Workflow
1 Like