How to update and reuse a variable throughout a workflow execution?

Describe the problem/error/question

I’m trying to set a variable at the beginning of my workflow, then update its value later, and finally reuse the updated version in downstream nodes — all within the same workflow execution.

For example: I initialize an array, append new values to it in multiple places, and want to access the final version at the end.

I’ve tried using:

  • this.setVariable() / this.getVariable()
  • $variables.set() / $variables.get()
  • Passing data manually via JSON from node to node (not scalable)

But so far, none of these approaches worked consistently.
What’s the correct way to store and update data throughout a single workflow execution across multiple nodes?

What is the error message (if any)?

When I try $variables.set() or $variables.get(), I get:

$variables is not defined

Please share your workflow

Share the output returned by the last node

Expected: The final updated value of the variable (e.g. updated array)
Actual: Can’t access or modify the variable reliably across nodes

Information on your n8n setup

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

If you’re on enterprise or cloud-pro, you can use $vars (but, fyi, those are read-only).

If you’re not on one of those licensed versions, $getWorkflowStaticData(type) might suit your needs.

1 Like

You can use edit node? to pass it down the flow, though?

Even in code node u should be able to do hits.

var originalUserId = $json[“userId”];

and call it from the last node? or call it from a node before like this

var originalUserId = $(‘Code1’).first().json.userId; // This gets ‘12345’ from Code1 not just last node ie $json

This is just a sample but you can also do this.
The IF node updates the value in the Edit Field node.
I’m not sure if this is a bug or an unknown feature but it works like a global var for a workflow.

Hello! Thank you for the responses, but I had to change the workflow because none of the proposed solutions worked. I ended up saving the information to the database and retrieving it later. I believe the suggested solutions only work on the paid version of n8n. But thanks anyway!

1 Like

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