{
  "name": "Nested Loops with Sub-workflows",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        80
      ],
      "id": "f7e5c300-fad6-4fc7-a7ff-79f7fac144f0",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "jsCode": "return [\n  {\n    \"color\": \"yellow\"\n  },\n  {\n    \"color\": \"blue\"\n  },\n  {\n    \"color\": \"green\"\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        80
      ],
      "id": "19f41efd-26ff-4950-9449-d52b8a98f040",
      "name": "Colors"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        448,
        80
      ],
      "id": "88109d36-2486-4b98-9377-893330490fb7",
      "name": "Loop Over Colors"
    },
    {
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "color"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        304
      ],
      "id": "c2b45eaa-eae7-45f6-b15f-6e863dedc71c",
      "name": "When Executed by Another Workflow"
    },
    {
      "parameters": {
        "jsCode": "return [\n  {\n    \"number\": 1\n  },\n  {\n    \"number\": 2\n  },\n  {\n    \"number\": 3\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        304
      ],
      "id": "96372e82-0484-441b-a6d5-12ce5a1ea921",
      "name": "Integers"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        448,
        304
      ],
      "id": "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8",
      "name": "Loop Over Integers"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3640152d-340b-4c27-b6f9-5735dcc93844",
              "name": "result",
              "value": "={{ $('When Executed by Another Workflow').item.json.color }} - {{ $('Loop Over Integers').item.json.number }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        672,
        304
      ],
      "id": "37b24ff0-165f-4a18-ae8a-c6054295fa87",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "content": "## Nested Loops with Sub-workflows\n### Template Description\nThis template provides a practical solution for a common n8n challenge: creating nested loops. While a powerful feature, n8n's standard **Loop** nodes don't work as expected in a nested structure. This template demonstrates the reliable workaround using a **main workflow** that calls a separate **sub-workflow** for each iteration.\n### Purpose\nThe template is designed to help you handle scenarios where you need to iterate through one list of data for every item in another list. This is a crucial pattern for combining or processing related data, ensuring your workflows are both clean and modular.\n### Instructions for Setup\n1. This template contains both the main workflow and the sub-workflow on a single canvas.\n2. **Copy the sub-workflow part** of this template (starting with the **Execute Sub-workflow Trigger** node) and paste it into a new, empty canvas.\n3. In the **Execute Sub-workflow** node in the **main workflow** on this canvas, update the `Sub-workflow` field to link to the new workflow you just created.\n4. Run the main workflow to see the solution in action.\n\n\nFor a detailed walkthrough of this solution, check out the [full blog post](https://n8nplaybook.com/post/2025/07/how-to-handle-nested-loops-in-n8n-with-sub-workflows/)",
        "height": 512,
        "width": 592
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        80
      ],
      "typeVersion": 1,
      "id": "679311c7-3401-4f5c-b08b-a6bbf7fee61b",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "WX9Oa7y6UMwGUYBM",
          "mode": "list",
          "cachedResultName": "Nested Loops with Sub-workflows"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {
            "color": "={{ $json.color }}"
          },
          "matchingColumns": [
            "color"
          ],
          "schema": [
            {
              "id": "color",
              "displayName": "color",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        },
        "options": {}
      },
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.2,
      "position": [
        672,
        80
      ],
      "id": "51a07d4a-a6f5-4061-99ad-3379411c643b",
      "name": "Execute Sub-workflow"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Colors": {
      "main": [
        [
          {
            "node": "Loop Over Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Colors": {
      "main": [
        [],
        [
          {
            "node": "Execute Sub-workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Integers": {
      "main": [
        [
          {
            "node": "Loop Over Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Integers": {
      "main": [
        [],
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Loop Over Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Sub-workflow": {
      "main": [
        [
          {
            "node": "Loop Over Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "63176d07-d2a5-49fb-90bb-4c6cd8910fd0",
  "id": "WX9Oa7y6UMwGUYBM",
  "tags": []
}