Bucles anidados usando sub-flujos de trabajo
Este es unEngineeringflujo de automatización del dominio deautomatización que contiene 9 nodos.Utiliza principalmente nodos como Set, Code, ManualTrigger, SplitInBatches, ExecuteWorkflow. Crear bucles anidados de procesamiento de datos usando sub-flujos de n8n
- •No hay requisitos previos especiales, puede importar y usarlo directamente
Nodos utilizados (9)
Categoría
{
"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
}
]
]
}
}
}¿Cómo usar este flujo de trabajo?
Copie el código de configuración JSON de arriba, cree un nuevo flujo de trabajo en su instancia de n8n y seleccione "Importar desde JSON", pegue la configuración y luego modifique la configuración de credenciales según sea necesario.
¿En qué escenarios es adecuado este flujo de trabajo?
Intermedio - Ingeniería
¿Es de pago?
Este flujo de trabajo es completamente gratuito, puede importarlo y usarlo directamente. Sin embargo, tenga en cuenta que los servicios de terceros utilizados en el flujo de trabajo (como la API de OpenAI) pueden requerir un pago por su cuenta.
Flujos de trabajo relacionados recomendados
Viktor Klepikovskyi
@vklepikovskyiCompartir este flujo de trabajo