Is there a reliable way to access the current item in a loop?

Hi folks,

Does anyone know if there’s a reliable way to access the item I’m currently looping over?

Here’s the workflow I’m working with:

At the very end of the workflow, I update a record in a spreadsheet. To do that, I need to access the row_number from the current item. However, I haven’t been able to find a simple and reliable way to do this.

Currently, I’m using the following workaround:

I store $input.item.json as a field in the “Format Data” node output:

let record = $input.item.json;
let thread = record.Body;

return {
  date: record.Date,
  subject: record.Subject,
  sender: extractEmail(record.From),
  thread: thread,
  item: $input.item
};

Then, in the “Update Record” node, I use this expression:

{{ $(‘Format data’).first().json.item.json.row_number }}

While this works, it feels fragile and easy to break.

I suspect the workflow would work better without the loop, but if I remove it, I’m unsure how to reliably get the row_number to update the spreadsheet.

Surely, there must be a straightforward way to access the item from previous nodes leading to the current step, right?

Any insights or suggestions would be much appreciated!

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:

n8n version: 1.66.0
Database (default: SQLite): default
n8n EXECUTIONS_PROCESS setting (default: own, main): didn’t change that
Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
Operating system: macOS 14.6.1

Welcome to the community @Vova_Nikulin !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


If you want to get the item from Loop node specifically, you can do that too bearing in mind that the node has 2 outputs and you need the data of the 2 output (branchIndex = 1, zero-based index). Your expression will look like this, {{ $('Loop Over Items').first(1).json.row_number }}.

This of cause implies that you iterate a single item in the loop.

See Output of other nodes | n8n Docs for details.

1 Like

Actually better to add No Operation node after the loop one to refer to it later with a $(noopnodename).last().json.row_number syntax. If the batch size of the loop is 1. In that case you don’t have to deal with branches

2 Likes

Thanks everyone for the comments. So far both suggestions look very similar to what I have now. Could you explain how is it better than what I have in current version?

There is still a scalability issue, I mean I cannot increase the batch size to speed up the processing. That gives me a feeling that I am doing something against the n8n design.

I got an extra question: I was also looking into itemMatching() function, but got a feeling that it is designed for the code section, do you know how to use it in other nodes?

@ihortom thanks for the tip, Attaching the code:

Both .item and .itemMatching() are meant for the same kind of reference, they are equivalent in this sense. The difference is, you would use .itemMatching() in the Code node specifically while .item would be used in any other node.

1 Like

Actually the .item works great in the Code node, so I dunno for what reason the itemMatching() is needed

Hi! Depending on what you are doing (if it’s a small ammount of data you want to hold between the loops) you can use the getWorkflowStaticData .

Bellow is a small example of how you can hold states between each loop. Let me know if you need more help to set this up!

It’s not a good idea to append something to the static data during the loop. A lot of items + no clearing before the loop = out of memory issue or database overloading with a high chance will happen

1 Like

Yes. That’s why I alerted about the ammount of data used. But still, only current way to have a state. And inside the “save_data_to_memory” it performs the clearing.

Not the best solution though, wasn’t build for this but works to get past a few issues.

Indeed, I could make use of getWorkflowStaticData in some cases.

The .item field is very useful and works in most scenarios, but it’s very fragile when you modify the workflow. For example, I encountered an issue when using it in a node with two entry points, such as the “Update Record” node in my example:

  • When it was called directly after the “If” node, it worked fine.
  • However, when it was called after another workflow, it failed with an error.

I somewhat understand why this happens, but I was hoping there might be a more reliable way to refer to the item we are currently iterating through in a loop. This has been the most challenging part for me so far because, without such a method, I need to retest and fix half of the workflow whenever I introduce a new node in the middle of it.

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