How to preserve index when using IF node

HI Everyone,
Hope you are happy and Healthy!

The problem I am facing is to preserve the Index while using SplitInBatches and If Node together,
I have prepared a scenario for the demonstration but let me give an overview first.

I am Reading data from google sheet or say from MySQL it’s huge data and I use SplitInBatches to break it down to batch of 10. Then I make an API call with HTTP node which may return 200 or any other code depending on data request i make.

After this I use IF node to check if result was 200 OK or anything else thus dividing the batch in random size(based on response of HTTP). Now I need to obtain the input value from Split in Batches which corresponds to the right index of the request so that I can mark in my databsae if API call for a row was success or Failure.

And at this point I fail as the index is no longer preserved when using IF node.

I have prepared the sample workflow to demonstrate the same.

Please check the Outcome from Set and Set1 under column “original” and compare it with the Input given to the SplitInBatches, you will notice the same index is repeated over and over for true and false both Set nodes.

Information on your n8n setup

  • n8n version: 0.163.1
  • Database you’re using (default: SQLite): MySQL
  • Running n8n with the execution process [own(default), main]: Not sure what this means
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: npm

You can try adding a set after the split with something like _pos =$position

Then when you want to reference the IF data from somewhere further down the line, use something like $item($json._pos).$node['IF'].json.

This is definitely a gotcha, and I believe people at n8n are looking into possible solutions. I’ve seen a number of people stumble over this picking up n8n, almost everyone makes the mistake once. Ideally we’d have some provenance tracking so we can say definitively that x piece of data originated from item y on node z.

2 Likes

Hi @pemontto,

Got stuck again and landed back on the same thread,

Can you please explain with an example, i was able to set the position but not access the position after the IF node.

Thank you!

Take a look at this. If you have a sufficiently new n8n and all your nodes have been upgraded to support it, you can do away with the Position ID set node and just use Paired Item method below.
edit 1: I just checked and the 2nd execution of the Set1 node produces the wrong output. @MutedJam, might be worth raising as a bug after confirming?
edit 2: no, after re-running with a full execution Paired Items is correct. Updating the workflow to correct the _pos result.

I updated Set and Set1 to include two new fields:

  • Set Original: {{ $item($json._pos).$node["Position ID"].json}}
  • Paired Item: {{ $("SplitInBatches").pairedItem().json }}
3 Likes

I was stuck on this for quite some time. I’m a newb. Your advice helped me get through it. Thanks so much! Hopefully, there will be a fix soon!

2 Likes

Awesome, Thank you so much.