Merge multiple runs into one not working as expected

Describe the problem/error/question

My goal is to merge multiple runs’ items into one run containing all individual items into a single list. I followed this example workflow. But the problem with this workflow is that if I use the Merge loop items Code node directly after a node that returns multiple runs, the merging works but the Code node then returns multiple runs too. To make this example work, you have to make the code node run for a single run done in previous node and the single run should be the last run of the previous node too (the example workflow also did this). In my example workflow you will see that I am trying to merge the runs of NoOp1 node using the Code node, but then the Code node returns 4 runs appending items in each run (notice the screenshot of output). For a workaround I am trying to return the last run using an If node and then run the code node for that last run so that it gives me the combined list of all runs (I have attached the workaround workflow code too).

What is the optimal solution to merge the runs into one run or if possible how do I get the last index of total runs executed in the previous node so that I can implement the workaround way dynamically instead of a fixed run value?

Thank you.

Please share your workflow

An example flow reproducing the problem:

Workaround flow where I use an if node which returns true for the last item and this lets the last Code node run once containing all the items of previous runs in a single list:

Share the output returned by the last node

Information on your n8n setup

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

hello @sinDbad

Why do you need to merge runs? The Loop node has a done branch where it will return all data from the iterations. You don’t have to do that code-merging-stuff

@barn4k My original workflow where I have to merge runs does not contain batch or loop node. I provided this workflow as an example to create multiple runs scenario and show where I am getting the issue. In my original flow multiple runs come from looping same nodes for different data (not using batch node). Hope you understand.

Anyways you can’t play with the multiple runs inside the loop. You can merge them only after you will finish the loop.

you can get the current run index with the variable {{ $runIndex }}

What is your original workflow? As the scenario that you are trying to implement seems very odd