How to use a nested split in batches?

Hello,

I’m wondering how to use nested split in batches.
The first function generates 10 items, the second function 5.
Now, I’d hoped that Set1 gets executed 50 times.
How to achieve this?

{
  "nodes": [
	{
	  "parameters": {},
	  "name": "Start",
	  "type": "n8n-nodes-base.start",
	  "typeVersion": 1,
	  "position": [
		300,
		280
	  ]
	},
	{
	  "parameters": {
		"functionCode": "const newItems = [];\n\nfor (let i=0;i<10;i++) {\n  newItems.push({json:{i}});\n}\n\nreturn newItems;"
	  },
	  "name": "Function",
	  "type": "n8n-nodes-base.function",
	  "position": [
		500,
		280
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"batchSize": 1,
		"options": {}
	  },
	  "name": "SplitInBatches",
	  "type": "n8n-nodes-base.splitInBatches",
	  "position": [
		700,
		280
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"conditions": {
		  "boolean": [
			{
			  "value1": true,
			  "value2": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}"
			}
		  ]
		}
	  },
	  "name": "IF",
	  "type": "n8n-nodes-base.if",
	  "position": [
		1460,
		810
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"keepOnlySet": true,
		"values": {
		  "string": [
			{
			  "name": "Message",
			  "value": "No Items Left"
			}
		  ]
		},
		"options": {}
	  },
	  "name": "Set",
	  "type": "n8n-nodes-base.set",
	  "position": [
		1670,
		810
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"functionCode": "const newItems2 = [];\n\nfor (let i=0;i<5;i++) {\n  newItems2.push({json:{i}});\n}\n\nreturn newItems2;"
	  },
	  "name": "Function1",
	  "type": "n8n-nodes-base.function",
	  "position": [
		900,
		450
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"batchSize": 1,
		"options": {}
	  },
	  "name": "SplitInBatches1",
	  "type": "n8n-nodes-base.splitInBatches",
	  "position": [
		1100,
		450
	  ],
	  "typeVersion": 1
	},
	{
	  "parameters": {
		"keepOnlySet": true,
		"values": {
		  "string": [
			{
			  "name": "id",
			  "value": "={{$node[\"SplitInBatches1\"].json[\"i\"]}}"
			}
		  ]
		},
		"options": {}
	  },
	  "name": "Set1",
	  "type": "n8n-nodes-base.set",
	  "position": [
		1170,
		810
	  ],
	  "typeVersion": 1
	}
  ],
  "connections": {
	"Start": {
	  "main": [
		[
		  {
			"node": "Function",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Function": {
	  "main": [
		[
		  {
			"node": "SplitInBatches",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"SplitInBatches": {
	  "main": [
		[
		  {
			"node": "Function1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"IF": {
	  "main": [
		[
		  {
			"node": "SplitInBatches",
			"type": "main",
			"index": 0
		  }
		],
		[
		  {
			"node": "SplitInBatches1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Function1": {
	  "main": [
		[
		  {
			"node": "SplitInBatches1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"SplitInBatches1": {
	  "main": [
		[
		  {
			"node": "Set1",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	},
	"Set1": {
	  "main": [
		[
		  {
			"node": "IF",
			"type": "main",
			"index": 0
		  }
		]
	  ]
	}
  }
}

Hey @Chris7935!

Welcome to the community :sparkling_heart:

I have updated your workflow and it now splits the data in the nested Split In Batches node correctly.

In the nested Split In Batches node I am using the Reset parameter. This parameter resets the value allowing the node to split values for the next items.

I hope this helps :slight_smile:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        260,
        280
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor (let i=0;i<10;i++) {\n  newItems.push({json:{i}});\n}\n\nreturn newItems;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "position": [
        550,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        750,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": true,
              "value2": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "position": [
        1510,
        510
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const newItems2 = [];\n\nfor (let i=0;i<5;i++) {\n  newItems2.push({json:{i}});\n}\n\nreturn newItems2;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "position": [
        950,
        450
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {
          "reset": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}"
        }
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1150,
        450
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "id",
              "value": "={{$node[\"SplitInBatches1\"].json[\"i\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "position": [
        1340,
        810
      ],
      "typeVersion": 1
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches1": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}