Set Node value not available to nodes after a function node

I have a set node that set’s a variable, and the following connected node get’s it just fine.

However, for nodes after a “function” node, I can no longer access the set variable, even if I call out the original node by name in the expression like this:

https://api.smartsheet.com/2.0/sheets/{{$node[“SS - Sheet ID”].json[“SheetID”]}}/rows?ids={{$json[“id”]}}&ignoreRowsNotFound=true

The problem value is:
{{$node[“SS - Sheet ID”].json[“SheetID”]}}

It just returns a “” value instead of the sheet ID I set.

For reference, this is the function code:

const newItems = [];

for (const element of items[0].json.rows) {
  newItems.push({json: element});
}

return newItems;

I realize this isn’t returning the original input items, and that might be part of the problem, but it seems like I should be able to access the set value regardless.

Am I doing something wrong? Seems like this should be working, thanks!

Is there a way to re-pass both the newItems and the sheetID from the function node?

Try with {{$item(0).$node["SS - Sheet ID"].json["SheetID"]}} instead of {{$node["SS - Sheet ID"].json["SheetID"]}}

1 Like

Did that work?

So interestingly, it doesn’t work for the earlier node, but that nomenclature works for the set node immediately preceeding the node the expression is in…

In the photo below, the SS - Sheet ID doesn’t come up as a valid expression value for node SS Delete Rows1, but SS - Sheet ID1 does.

Yes, because that depends. You probably need to set the ID no matter the number of inputs in one, and in the other, you do not. Can you share the workflow so I can have a better look?

Also, it looks like the split batches node there it’s not needed. You might want to check how looping works in n8n.

Here’s the workflow.

The basic idea is this:

  1. Get all the data from smartsheets
  2. Use the node “Transform JSON” to remove the upper-most level of irrelevant json, since I just need the column ID’s array.
    3- Split the column ID array into batches of 50 items, since the API call is passed that as a comma seperated list in the query header, limiting the number of ID’s that can be passed without breaking the header size. (Technically, I have been able to do up to 250, but with this sheet, it results in API timeouts due to the size, so I had to reduce it to batches of 50).
    4 - Use the “Convert ID’s” function to change the JSON array into a comma separated list.
    5 - Test the list to see if it’s nil (because then it’s done)
    6 - Use the Set Sheet ID1 node to re-set the sheet ID
    7 - Delete the batch of rows
    8 - repeat with next batch.
{
  "nodes": [
{
  "parameters": {
    "authentication": "headerAuth",
    "url": "=https://api.smartsheet.com/2.0/sheets/{{$json[\"SheetID\"]}}",
    "options": {
      "splitIntoItems": false
    },
    "queryParametersUi": {
      "parameter": [
        {
          "name": "columnIds",
          "value": "={{$json[\"columnIds\"]}}"
        }
      ]
    }
  },
  "name": "SS Get Sheet",
  "type": "n8n-nodes-base.httpRequest",
  "typeVersion": 1,
  "position": [
    40,
    -600
  ],
  "credentials": {
    "httpHeaderAuth": "Smartsheet Integromat"
  }
},
{
  "parameters": {
    "authentication": "headerAuth",
    "requestMethod": "DELETE",
    "url": "=https://api.smartsheet.com/2.0/sheets/{{$item(0).$node[\"SS - Set Sheet ID\"].json[\"SheetID\"]}}/rows?ids={{$json[\"therows\"]}}&ignoreRowsNotFound=true",
    "jsonParameters": true,
    "options": {}
  },
  "name": "SS Delete Rows1",
  "type": "n8n-nodes-base.httpRequest",
  "typeVersion": 1,
  "position": [
    1270,
    -430
  ],
  "credentials": {
    "httpHeaderAuth": "Smartsheet"
  }
},
{
  "parameters": {},
  "name": "NoOp",
  "type": "n8n-nodes-base.noOp",
  "typeVersion": 1,
  "position": [
    1250,
    -600
  ]
},
{
  "parameters": {
    "batchSize": 50,
    "options": {
      "reset": false
    }
  },
  "name": "SplitInBatches",
  "type": "n8n-nodes-base.splitInBatches",
  "typeVersion": 1,
  "position": [
    430,
    -600
  ],
  "alwaysOutputData": true
},
{
  "parameters": {
    "functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\n\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\nconst newItems = [];\n\nfor (const element of items[0].json.rows) {\n  newItems.push({json:element});\n}\n\n// const theData = newItems.map(element => ({ json: element }))\n\nreturn newItems;\n"
  },
  "name": "Transform JSON to Row IDs Only2",
  "type": "n8n-nodes-base.function",
  "typeVersion": 1,
  "position": [
    240,
    -600
  ],
  "alwaysOutputData": true
},
{
  "parameters": {
    "functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\n\nreturn [\n  {\n    json: {\n      therows: items.map(e => e.json.id).join(',')\n    },\n  }\n]\n"
  },
  "name": "Convert ID's to Comma String2",
  "type": "n8n-nodes-base.function",
  "typeVersion": 1,
  "position": [
    640,
    -600
  ]
},
{
  "parameters": {
    "conditions": {
      "string": [
        {
          "value1": "={{$json[\"therows\"]}}",
          "operation": "isEmpty"
        }
      ]
    }
  },
  "name": "Test Empty2",
  "type": "n8n-nodes-base.if",
  "typeVersion": 1,
  "position": [
    870,
    -600
  ]
},
{
  "parameters": {
    "values": {
      "string": [
        {
          "name": "SheetID",
          "value": "123456789"
        },
        {
          "name": "columnIds",
          "value": "968346995844996"
        }
      ],
      "number": []
    },
    "options": {}
  },
  "name": "SS - Set Sheet ID",
  "type": "n8n-nodes-base.set",
  "typeVersion": 1,
  "position": [
    -150,
    -600
  ],
  "alwaysOutputData": true
},
{
  "parameters": {
    "values": {
      "string": [
        {
          "name": "SheetID",
          "value": "={{$node[\"SS - Set Sheet ID\"].json[\"SheetID\"]}}"
        }
      ],
      "number": []
    },
    "options": {}
  },
  "name": "SS - Set Sheet ID1",
  "type": "n8n-nodes-base.set",
  "typeVersion": 1,
  "position": [
    1070,
    -430
  ],
  "alwaysOutputData": true
},
{
  "parameters": {
    "path": "72927bb8-2368-488f-9bfc",
    "options": {}
  },
  "name": "Webhook",
  "type": "n8n-nodes-base.webhook",
  "typeVersion": 1,
  "position": [
    -430,
    -600
  ],
  "webhookId": "72927bb8-",
  "disabled": false
}
  ],
  "connections": {
"SS Get Sheet": {
  "main": [
    [
      {
        "node": "Transform JSON to Row IDs Only2",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"SS Delete Rows1": {
  "main": [
    [
      {
        "node": "SplitInBatches",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"SplitInBatches": {
  "main": [
    [
      {
        "node": "Convert ID's to Comma String2",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"Transform JSON to Row IDs Only2": {
  "main": [
    [
      {
        "node": "SplitInBatches",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"Convert ID's to Comma String2": {
  "main": [
    [
      {
        "node": "Test Empty2",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"Test Empty2": {
  "main": [
    [
      {
        "node": "NoOp",
        "type": "main",
        "index": 0
      }
    ],
    [
      {
        "node": "SS - Set Sheet ID1",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"SS - Set Sheet ID": {
  "main": [
    [
      {
        "node": "SS Get Sheet",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"SS - Set Sheet ID1": {
  "main": [
    [
      {
        "node": "SS Delete Rows1",
        "type": "main",
        "index": 0
      }
    ]
  ]
},
"Webhook": {
  "main": [
    [
      {
        "node": "SS - Set Sheet ID",
        "type": "main",
        "index": 0
      }
    ]
  ]
}
  }
}

Hey @wsargent,

Were you able to find a solution? Let us know if you still need help :slight_smile:

Hi Harshil, yes the solution is to add the $item(“0”). onto the front of the normal expression as follows:

{{ $item(“0”).$node[“Settings”].json[“baseURL”] }}

It seems to me like this is a bug, or that this should be default behaviour when picking a field in the expression builder, from a node that is not the “input” node.

However, this works. Thanks!

1 Like