How to Pass Variables Between Nodes (Especially for Distant Nodes)?

I’m building a workflow where I need to pass variables between nodes, especially when those nodes are not directly connected. I’ve encountered issues where some nodes don’t execute, and when I try to reference the data in a later node, it gets stuck or returns undefined.

What I’ve Tried So Far:

  1. Set Node – I attempted to store variables using the Set Node, but because it’s separated from the main execution flow, some instances of the node didn’t execute. As a result, when a later node tried to reference the variable, the data wasn’t available, causing issues.

  2. Referencing with $node[“Node Name”].json[“key”] – This works in some cases, but when nodes are not executed due to being outside the main flow, the values are missing.

  3. Merge Node – This helps when combining multiple data sources, but it doesn’t solve cases where I need to persist data for later use in a disconnected node.

My Question:

What is the best way to store and pass variables between nodes that are not directly connected?

How can I ensure that variables persist correctly and are reliably accessible, even when the node that sets them is not part of the main execution flow?

If anyone has encountered and solved this issue in n8n, I’d appreciate your insights! Thanks in advance. :blush:

1 Like

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

dont just activate your own flows on me… :rofl:
that is a general question that wants to understand how to use the platform in the most correct way. currently, I couldn’t find any answers in the documentation or any other source…

Just asking the community for best practices… :slight_smile:
Thanks…

Multi-branch workflow follows an execution order from top to bottom.
Meaning a branch from the top will execute before the one in the bottom.

During testing that doesn’t happen, but during execution every branch gets executed.

So theoretically you could retrieve values from nodes that are not in the same branch.

But ideally you want to pass data to the same branch. Because you could lose the items reference if you mess up the data (see item linking).

The best way to pass data from multiple branches forward is indeed the Merge node. It is a very flexible node that allows you to merge data in many ways.

Alternatively you could use the Code node with a custom written JavaScript or Python code. You can ask ChatGPT to generate that for you.

I hope this helps.

If my reply answers your question, please remember to mark it as a solution.

Thanks for the detailed reply! :slight_smile:

So, lets say, that I have the following flow:

I dont want to connect everything to the final node because then, it activates the final node before others finished (since it is part of the upper branch).

What is the actual syntax then, that I can use in my final ‘HTML’ node, to reference the data that is waiting in the ‘Hold article’ (Set) node? No matter what I do - I cant reference that variable without actually connecting the node, which then - breaks my flow too.

Connect everything in a single branch, instead of separating them.
Like this:

.

The “Edit Fields” node allows you to pass previous data forward:

That way everything arrives at the final destination, but triggers the final node only once per item.

Let me know if that solves your problem! :muscle:

1 Like

In your example you don’t actually need to branch off just like gmsalomao2 suggested.

If you do need branches for different logic though you can have multiple workflows and use the Execute Sub-Workflow node and trigger.

For example this workflow checks if a summary of an Airtable record is empty.
If it is it generates it and calls a sub-workflow.
If it’s not it goes directly to the same sub-workflow.

Initially i did it just like you did and merged back in but i ran into the same issue.

Beside the sub-workflow split i know there are custom variables that you can set with very simple code node Create custom variables | n8n Docs but they are available only in the higher tier paid plans

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.