Utiliser des boucles imbriquées avec des sous-workflows

Intermédiaire

Ceci est unEngineeringworkflow d'automatisation du domainecontenant 9 nœuds.Utilise principalement des nœuds comme Set, Code, ManualTrigger, SplitInBatches, ExecuteWorkflow. Utiliser des sous-workflows n8n pour créer des boucles de traitement de données imbriquées

Prérequis
  • Aucun prérequis spécial, prêt à l'emploi après importation

Catégorie

Aperçu du workflow
Visualisation des connexions entre les nœuds, avec support du zoom et du déplacement
Exporter le workflow
Copiez la configuration JSON suivante dans n8n pour importer et utiliser ce workflow
{
  "id": "WX9Oa7y6UMwGUYBM",
  "meta": {
    "instanceId": "8981479cb588889c05b145eaed421551d37a4fff11ab279d3f4744a6577c6002"
  },
  "name": "Nested Loops with Sub-workflows",
  "tags": [],
  "nodes": [
    {
      "id": "f7e5c300-fad6-4fc7-a7ff-79f7fac144f0",
      "name": "When clicking ‘Execute workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        0,
        80
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "19f41efd-26ff-4950-9449-d52b8a98f040",
      "name": "Colors",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        80
      ],
      "parameters": {
        "jsCode": "return [\n  {\n    \"color\": \"yellow\"\n  },\n  {\n    \"color\": \"blue\"\n  },\n  {\n    \"color\": \"green\"\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "88109d36-2486-4b98-9377-893330490fb7",
      "name": "Loop Over Colors",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        448,
        80
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "c2b45eaa-eae7-45f6-b15f-6e863dedc71c",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        0,
        304
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "color"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "96372e82-0484-441b-a6d5-12ce5a1ea921",
      "name": "Integers",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        304
      ],
      "parameters": {
        "jsCode": "return [\n  {\n    \"number\": 1\n  },\n  {\n    \"number\": 2\n  },\n  {\n    \"number\": 3\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8",
      "name": "Loop Over Integers",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        448,
        304
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "37b24ff0-165f-4a18-ae8a-c6054295fa87",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        672,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3640152d-340b-4c27-b6f9-5735dcc93844",
              "name": "result",
              "type": "string",
              "value": "={{ $('When Executed by Another Workflow').item.json.color }} - {{ $('Loop Over Integers').item.json.number }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "679311c7-3401-4f5c-b08b-a6bbf7fee61b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        80
      ],
      "parameters": {
        "width": 592,
        "height": 512,
        "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/)"
      },
      "typeVersion": 1
    },
    {
      "id": "51a07d4a-a6f5-4061-99ad-3379411c643b",
      "name": "Execute Sub-workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        672,
        80
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "WX9Oa7y6UMwGUYBM",
          "cachedResultName": "Nested Loops with Sub-workflows"
        },
        "workflowInputs": {
          "value": {
            "color": "={{ $json.color }}"
          },
          "schema": [
            {
              "id": "color",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "color",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "color"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "63176d07-d2a5-49fb-90bb-4c6cd8910fd0",
  "connections": {
    "19f41efd-26ff-4950-9449-d52b8a98f040": {
      "main": [
        [
          {
            "node": "88109d36-2486-4b98-9377-893330490fb7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "96372e82-0484-441b-a6d5-12ce5a1ea921": {
      "main": [
        [
          {
            "node": "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "37b24ff0-165f-4a18-ae8a-c6054295fa87": {
      "main": [
        [
          {
            "node": "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "88109d36-2486-4b98-9377-893330490fb7": {
      "main": [
        [],
        [
          {
            "node": "51a07d4a-a6f5-4061-99ad-3379411c643b",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8": {
      "main": [
        [],
        [
          {
            "node": "37b24ff0-165f-4a18-ae8a-c6054295fa87",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "51a07d4a-a6f5-4061-99ad-3379411c643b": {
      "main": [
        [
          {
            "node": "88109d36-2486-4b98-9377-893330490fb7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "c2b45eaa-eae7-45f6-b15f-6e863dedc71c": {
      "main": [
        [
          {
            "node": "96372e82-0484-441b-a6d5-12ce5a1ea921",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "f7e5c300-fad6-4fc7-a7ff-79f7fac144f0": {
      "main": [
        [
          {
            "node": "19f41efd-26ff-4950-9449-d52b8a98f040",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Foire aux questions

Comment utiliser ce workflow ?

Copiez le code de configuration JSON ci-dessus, créez un nouveau workflow dans votre instance n8n et sélectionnez "Importer depuis le JSON", collez la configuration et modifiez les paramètres d'authentification selon vos besoins.

Dans quelles scénarios ce workflow est-il adapté ?

Intermédiaire - Ingénierie

Est-ce payant ?

Ce workflow est entièrement gratuit et peut être utilisé directement. Veuillez noter que les services tiers utilisés dans le workflow (comme l'API OpenAI) peuvent nécessiter un paiement de votre part.

Informations sur le workflow
Niveau de difficulté
Intermédiaire
Nombre de nœuds9
Catégorie1
Types de nœuds7
Description de la difficulté

Adapté aux utilisateurs expérimentés, avec des workflows de complexité moyenne contenant 6-15 nœuds

Liens externes
Voir sur n8n.io

Partager ce workflow

Catégories

Catégories: 34