# Incrementing Node Value Using Custom Scripts

**URL:** <https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471>\
**Category:** Questions\
**Tags:** node\
**Created:** [November 27, 2023, 4:10pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471 "2023-11-27T16:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leosantosx](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/leosantosx/32/5901_2.png) [@leosantosx](https://community.n8n.io/u/leosantosx)\
**Post date:** [November 27, 2023, 4:10pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471/1 "2023-11-27T16:10:05Z")

</div>

Hi, I have a question. Currently, I have a node that performs a number increment. I am setting this node with an initial value of 0 and want to increment a number in it.

My node component:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/c/b/cbb4217b89c5525f10d17ea971fcd2ca6dcdac9f.png)

Example of my code:  
Previously, I used the following, and it worked perfectly:  
`$item(0).$node["Set counter"].json["counter"]`

I’ve tried other ways, such as:  
`$('Set counter').first().json.counter += 1 // Increment counter`

But it didn’t work.  
The value is changed, but the state is not maintained. Instead of being altered, it remains at only the value of 1

For the workflow I am working on, this seems to be the only solution. I tried a few others, but they didn’t work. I need to change the value of the node directly using custom node scripts

## Information on your n8n setup

- \*\*n8n version: 0.224.2
- \*\*Database (default: SQLite): postgres
- \*\*n8n EXECUTIONS\_PROCESS setting (default: own, main): default
- \*\*Running n8n via (Docker, npm, n8n cloud, desktop app): docker
- \*\*Operating system: Ubuntu Server / docker container

---

<div class="post-metadata">

**Author:** ![n8n](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/n8n/32/196425_2.png) [@n8n](https://community.n8n.io/u/n8n)\
**Post date:** [November 27, 2023, 4:10pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471/2 "2023-11-27T16:10:06Z")

</div>

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:**

---

<div class="post-metadata">

**Author:** ![leosantosx](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/leosantosx/32/5901_2.png) [@leosantosx](https://community.n8n.io/u/leosantosx)\
**Post date:** [November 27, 2023, 4:33pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471/3 "2023-11-27T16:33:56Z")

</div>

My old code worked, after the update n8n version it didn’t work:

```auto

items[0].json.lastNode = false;

// Increment counter
$item(0).$node["Set counter"].json["counter"] += 1;

// Check if the current node is the last node
const lastNode = $items("calculate-voucher-quantity", 0, 0).length <= $item(0).$node["Set counter"].json["counter"];

// If it's the last node, set lastNode to true
if (lastNode) {
  items[0].json.lastNode = true;
}

// Return the items array
return items;

```

---

<div class="post-metadata">

**Author:** ![leosantosx](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/leosantosx/32/5901_2.png) [@leosantosx](https://community.n8n.io/u/leosantosx)\
**Post date:** [November 27, 2023, 7:06pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471/4 "2023-11-27T19:06:02Z")

</div>

I was able to solve it. 🙌😅  
I used the variable $runIndex, and the code looked like this:

```auto
const isLastNode = $items("node_name_here", 0, 0).length <= $runIndex + 1

items[0].json.lastNode = isLastNode

return items;

```

Workflow example:

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [December 4, 2023, 7:06pm UTC](https://community.n8n.io/t/incrementing-node-value-using-custom-scripts/33471/5 "2023-12-04T19:06:20Z")

</div>

Closed on December 4, 2023, 7:06pm UTC

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