Executing workflows dynamically

Hi Everyone,

I am trying to set the execute workflow Id dynamically from an array that i am passing in. i would like it to execute a workflow for each item in the array as the workflowId.

a basic example would be below which obviously doesnt work.

	
name	"My workflow 3"
nodes	
0	
parameters	{}
name	"Start"
type	"n8n-nodes-base.start"
typeVersion	1
position	
0	-90
1	50
1	
parameters	
source	"=parameter"
workflowJson	"= "
name	"Execute Workflow"
type	"n8n-nodes-base.executeWorkflow"
typeVersion	1
position	
0	320
1	40
2	
parameters	
functionCode	"// ipData = $node[\"recivedNode\"].json[\"data\"];\nlet data = [5,6,7,8];\n\n\nreturn data\n"
name	"Function1"
type	"n8n-nodes-base.function"
typeVersion	1
position	
0	120
1	40
alwaysOutputData	true
connections	
Start	
main	
0	
0	
node	"Function1"
type	"main"
index	0
Function1	
main	
0	
0	
node	"Execute Workflow"
type	"main"
index	0
active	false
settings	
saveManualExecutions	true
executionTimeout	-1
id	7

any help would be greatly appreciated.

Hi @jjlittle201ps, welcome to the community!

It looks like part of your workflow JSON got missing when pasting it on the forum. Maybe you can double-check this?

Setting the workflow ID ondynamically should nevertheless work, in this example my Execute Workflow node executes the workflow using an ID it receives from the previous Set node:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "workflow_id",
              "value": 1032
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "workflowId": "={{$json[\"workflow_id\"]}}"
      },
      "name": "Execute Workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Execute Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

hey thanks for your quick response!

I took your example and tweaked it a little but i am getting and error that the ids dont exist

{
  "name": "My workflow 3",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 5\n    }\n  },\n    {\n    json: {\n      id: 6\n    }\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        440,
        300
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "workflow_id",
              "value": "= {{$node[\"Function\"].json[\"url\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "workflowId": "={{$node[\"Set\"].json[\"workflow_id\"]}}"
      },
      "name": "Execute Workflow1",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1,
      "position": [
        830,
        300
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Execute Workflow1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "saveManualExecutions": true,
    "executionTimeout": -1
  },
  "id": 8
}

basically want it to fire off a workflow for each of the ids i specify.

thanks for taking the time to help me.

Ah, thanks very much for clarifying!

So in your example it seems you are trying to set the respective workflow IDs in the id field which is working fine (I am using different IDs because I don’t have workflows with IDs 5 or 6 on my end):
image

Since this is all the information the Execute Workflow node needs, you wouldn’t need the Set node in this example case. There is, however, one caveat which might not be obvious when using the Execute Workflow node: It requires creating a loop manually (virtually all nodes run once for each item they receive, but there are some exceptions). So to run multiple different workflows, you would need to use the Split In Batches node to split your input data into “batches” of 1 like so:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        200
      ]
    },
    {
      "parameters": {
        "workflowId": "={{$json[\"id\"]}}"
      },
      "name": "Execute Workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1,
      "position": [
        850,
        50
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 1032\n    }\n  },\n    {\n    json: {\n      id: 1034\n    }\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        200
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        650,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Execute Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

You can then access the result for each execution through the Data of Execution dropdown field in the Execute Workflow node:
image

hey

that seems to work great however when i check the execution logs(i have everything selected to log ) it doesnt show the dynamic workflows being triggered.

thanks again

You would need to enable saving manual executions for each involved workflow in order for this to happen.

Here’s an example “Execute Workflow with ID from Previous Node” is my workflow executing the other workflows showing the difference. In case 1 I had saving manual executions enabled for both workflows called. In case 2 I disabled saving manual executions.

Hi again,

Thankyou so much for your help. I got it all working now. You are a hero!

Kind Regards

1 Like

Sweet, I am happy to hear that. Thanks so much for confirming!

Hi,

just a quick question would i be able to pass data along side the workflow id i specify that will only get passed to the specific workflow i trigger. so if i had something similar to this -

return [
  {
    json: {
      id: 5,
      data:"I am some important data related to id 5 "
    }
  },
    {
    json: {
      id: 6
      data:"I am some different data related to id 6"
    }
  }
]

would it filter out the data related to that id or just pas the whole block to each workflow that is triggered?

Thanks again

That example structure will pretty much do what you have in mind. From the docs:

The Execute Workflow node passes the data to the Start node of Workflow B.

So the executed workflow will start with the respective data field. In other words, your workflow 5 would start with a data value of I am some important data related to id 5 while your workflow 6 would be started a data value of I am some different data related to id 6 in this example.

excellent thanks again!

1 Like