How do you use global variables in the Code node?

I am currently on the 14 day trial version of web n8n. Maybe that’s why I’m having problems…

I’m having trouble setting the value of a global variable, but I have no problem reading the variable. My goal is to be able to save the most recent timestamp of one of the data items, to prevent reusing old data and only get what’s new. The workflow may run on a daily schedule.

The key location is the first Filter and Code nodes. The Filter removes items which are older than the current latest date ($vars.lastCveTimestamp). The Code updates the date for the next cycle.

$vars.lastCveTimestamp is set by default to 0 and represents milliseconds since January 1, 1970. Typical JavaScript date timestamp.

The Filter is working and it’s not throwing an error, but the Code is not updating that global variable.

What’s the correct way to do this?

The value of $vars.lastCveTimestamp after running this workflow when I go to the Variables tab on n8n:


Information on my n8n setup

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

Hello @fivemoreminix welcome to our forums!

It’s good you mentioned this, the variables cannot be updated programatically, they’re supposed to be “read only” so n8n won’t complain if you try to override the value, but it won’t save it. We should update the docs to reflect that it’s not updatable.

For your use case, I think “static data” is what you’re looking for: getWorkflowStaticData | n8n Docs

Static data is a per-workflow value that is stored across executions.

1 Like

That looks perfect, thanks!

Hopefully the documentation gets updated to explain that, because I would think it’s a pretty important part of an algorithm. But I couldn’t find this answer online.

Thanks again

Edit: I never saw the “Code in n8n” section of the documentation. My fault!

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