Is there something like global variable?

I want to have global variables when the workflow starts and be able to change a variable in the next node or add a new variable, is it possible?
I think of using set node when the workflow starts and he will act as global variables

1 Like

I think your approach of using a Set node might be the easiest (it’s what I tend to do in such cases). There is, however, also static workflow data which can be accessed through Function and FunctionItem nodes: Function | Docs, Function Item | Docs. This data would be stored across executions (which otherwise would not be possible).

do you have an example?

You mean for abusing the Set node as data storage? Sure:

First Set Node just creates an empty String value:

image

The Function nodes then add stuff to this particular value of the first (and only) item of the Set node:
image

The final Set node just writes the resulting string in a new result field.

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "myValue"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "$item(0).$node[\"Set\"].json[\"myValue\"] += \"Foo\";\n\nreturn items;"
      },
      "name": "Do Something",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "$item(0).$node[\"Set\"].json[\"myValue\"] += \"Bar\";\n\nreturn items;"
      },
      "name": "Do More",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "result",
              "value": "={{$node[\"Set\"].json[\"myValue\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Get Result",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "$item(0).$node[\"Set\"].json[\"myValue\"] += \"Baz\";\n\nreturn items;"
      },
      "name": "One More",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Do Something",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Do Something": {
      "main": [
        [
          {
            "node": "Do More",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Do More": {
      "main": [
        [
          {
            "node": "One More",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One More": {
      "main": [
        [
          {
            "node": "Get Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I use this very rarely though. In my only real case I put together a text-based report. The string field of the initial Set node just holds the result while all subsequent nodes fetch data and add it to the initial string. In most cases this wouldn’t be necessary because you can just write directly to (for example) a Google Doc

Edit: And even in the case described it’s not technically necessary (as I could just fetch the results from each node at the very end of my workflow). But it’s possible if needed :smiley:

Thanks that was very helpful