Pass Parameters to Execute Workflow

Is there a way to pass variables to a “Sub workflow”?

1 Like

There is no way to pass in variables but you can pass in flow-data.

The input data of the “Execute Workflow Node” is at the same time the output of the Start-Node in the called workflow. The output data of the last node of the executed workflow will be the same as the output of the “Execute Workflow” Node.

so that would be $node[“Start”].data[""]?

Yes exactly. The expression on a parameter would be something like:

{{$node["Start"].data["propertyName"]}}

it does not work for me, it is probably something i am doing wrong :smiley:
here are the details:


Hm, strange. So both nodes are in the sub-workflow and they are connected?

they are connected but not following:

I just tested. It works great for me.

Main workflow (do not forget to change the worklow ID):

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "workflowId": "209"
      },
      "name": "Execute Workflow Database",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "test",
              "value": "myData"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Execute Workflow Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Sub-Workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        450
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "value": "={{$node[\"Start\"].data[\"test\"]}}"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

ok, i see it works, but i’m calling it after a ‘\’ because this is a file path. maybe i need to escape it somehow?

Yes, it seems like the backslash is causing problems. It works in the UI but somehow when it gets executed it messes things up:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "test",
              "value": "asdf"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        780,
        320
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "value": "=C:\\User\\{{$node[\"Set\"].data[\"test\"]}}_whatever.txt"
            }
          ]
        }
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        930,
        320
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

So for now I guess you have to “create” the value in a Function-Node in then reference it in the node you need it like in this example:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "test",
              "value": "asdf"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        820,
        340
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "value": "={{$node[\"Function\"].data[\"myPath\"]}}"
            }
          ]
        }
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1130,
        340
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.myPath = 'C:\\User\\\\' + items[0].json.test + '_whatever.txt';\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        970,
        340
      ]
    }
  ],
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Nice. i will do that. thanks!
should i raise a but for it?

n8n is using under the hood the great riot-tmpl module for expressions. So it is actually going “wrong” in there. Just opened in issue there, asking if it is really a bug or if I can fix it with some configuration:

1 Like

Hi,

First off thanks for this great project. So far I have been copying JSON to re-use workflows. Executing a child workflow with variables sounds a lot better.

At the moment, did I understand well that the “caller” needs to know/find out all references to $node[‘start’].data manually? I think the proposal or adding explicit variables is interesting.

They could be added as visible properties to the Start node, and read by Execute Workflow to prompt for values. Triggering a flow with variables directly could either fail, or prompt for values for debugging.

This is how NodeRed handles subflows, and I think explicit variables makes it very easy to discover the feature, and robust as the caller is given a clear interface (without having to look under the hood).

Of course it could be optional and the start flow could still inherit all of the caller’s output.

Hello @romain! Welcome to the community.

Yes, you understand that correctly. For now, I would simply copy example input data and then paste it in a Function-Node after the Start-Node. Best to add a NoOp-Node after the Function one to be able to reference data from that node (to have one fixed start-point). You can then always when you want to test, simply activate/deactivate the Function-Node.

Thanks a lot for the input. It is very helpful! It is planned to add something similar to what you described to make the process easier in the future. As like you realized it is currently not optimal. Sadly have however no estimate yet as the workaround (even though it is not great) is not too complicated and I want to make sure that the functionality we implement here can also be used for another realted feature that is planned.

Thanks for the idea, the “sample” function node is a good starting workaround for now. I will keep an eye on announcements in the future! Thanks

Just wanted to share that the approach worked well, thank you.
I can just think of two nice-to-have things that would help with legibility:

  • being able to set the required variables from within “Execute Workflow” itself
  • displaying the workflow name instead of the ID underneath

To illustrate, this is the corresponding NodeRed equivalent where the purpose of the child workflow is obvious:

subflow-n8n
subflow-nodered

Sorry about the comparison, I understand the products are quite different but subflows is one of the things it does quite well.

Great to hear that it works well!

Yes agree displaying the Workflow-Name instead of the ID would be much nicer. That is however technically not possible. Or sure it would be possible but would require quite some work which it would simply not be worth right now.

Thanks a lot, will check how Node-Red handles that.

1 Like