Issue with Nested Loops

The Problem

I have a workflow with a nested loop structure where:

  1. The outer loop processes different client types and generates unique content for each
  2. The inner loop should process this content through translations for different languages

However, when the outer loop moves to its second iteration, the inner language loop fails to process the languages, seemingly thinking it has already processed these items.

What I’ve Tried

  1. Adding unique identifiers to languages using expressions:
{{ $('On form submission').item.json.Languages.map(lang => lang + '_' + $now()) }}

And then cleaning them up when referenced:

{{ $('Loop Over Items').item.json.Languages.split('_')[0] }}
  1. Structuring the language data to include unique combinations of client type and language

However, even with these attempts at making each iteration’s data unique, the inner loop still fails to process languages on the second iteration of the outer loop.

Expected Behavior

  • First iteration: Process client type 1’s content through all languages
  • Second iteration: Process client type 2’s content through all languages
  • And so on…

Actual Behavior

  • First iteration: Works correctly
  • Second iteration: Inner loop doesn’t process the languages, seems to think they’re already processed

Question

How can I structure this workflow so that the inner language loop properly processes all languages for each iteration of the outer client type loop? Is there a different approach to handling nested loops that would work better for this use case?

Any help or suggestions would be greatly appreciated!

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @dan1

That’s because you can’t use nested loops via the Loop Node, you should move the inner loop into the sub workflow. Or you can move both loops into sub workflows (e.g. main WF > sub WF as an outer loop > sub-sub WF as an inner loop)

2 Likes

Ok, wow, I had no clue. But how do I reference data in the subworkflow from the main workflow?

Output everything you need for the sub workflow just before the Execute Workflow node

1 Like

Thank you!

Last question: what is the proper way to do this in my case? I had a “Split Out” node before the Loop node, and it made sense. Here, it seems like I need to have a “Set” or “Code” node before the “Execution Workflow” to pass on the data, but how would it know which items to loop over if I can’t have a “Split Out” before the Loop node?

This logic is a bit challenging for me.

Place a Code/Edit fields node after the Split Out node and add additional fields which you may need for the sub workflow to do it’s work.

Most likely you will need to specify variables from other nodes with syntax $('nodename').last().json.variableName or $('nodename').first().json.variableName.

Because the syntax $('nodename').item.json.variableName won’t work if the number of items on the choosen node doesn’t match with the number of items of the target ‘nodename’ node (or if any node inbetween has a different number of items)

1 Like

Thank you!