Test sub-workflow with data from trigger workflow

Took me a bit to wrap my head around how to best test my sub-workflows, without having to hop tabs constantly triggering the parent workflow just to have the data come through!

Also this way I can use the visual editor while referencing live execution data, which is a far more enjoyable development experience.

1. Add 2 nodes to your subworkflow

Copy astrong textnd paste the code below into your subworkflow to add the two nodes “Set Init-Data” and “Input Data”.
image

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{\n\"json\": {\n    \"YourKey\": \"Your Value\",\n    \"ReplaceThese\": \"With the output of the last node before triggering this workflow.\",\n    \"MakeSure\": \"To not paste the outer [{ CONTENT }], just the CONTENT part.\",\n    \"Then\": \"You can reference data like {{$node[\"Input Data\"].json[\"YourKey\"]}}\",\n    \"And\": \"just deactivate the \"Set Init-Data\" node when ready for production!\"\n  }\n}]"
      },
      "name": "Set Init-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        580,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Input Data",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    }
  ],
  "connections": {
    "Set Init-Data": {
      "main": [
        [
          {
            "node": "Input Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

2. Copy data from parent workflow

Copy the output of the node in the parent workflow, just before triggering the subworkflow.
Only grab the CONTENT part between the outer brackets and curly braces [{ CONTENT }].

3. Paste in “Set Init-Data” node

Should be obvious what to replace, open the node and read the text.

4. Develop your subworkflow

Without having to jump between workflows. Reference the “Input Data” node in expressions, so the reference can stay the same in production.
{{$node["Input Data"].json["YourKey"]}}

5. Test in production

Simply deactivate the “Set Init-Data” node and test by triggering your parent workflow.
Should work like a breeze!

Activate each workflows settings to save manual executions, so you can check that the executions worked well.


I got the idea of that approach here in the forums, I think from a N8N team member, so due credit to them!! Just wanted to give back and share this, now that I have figured it out. I’m using this approach and code in my own workflows, so hopefully it helps somebody here in the N8N Nation <3

3 Likes