How to update value when in loop

Describe the problem/error/question

For microsoft mssql i need to implement a pagination using OFFSET i FETCH NEXT j ROWS ONLY
After i get the batch of items, i used Split in Batches node (i need them one by one, simplified the original workflow here). After the Split in Batches Done, i tried to update the offset variable in the UpdateOffset node;

heres the expression:

{{ $('IF').item.json.offset + $('Variables').item.json.batch_size }}

it seems that $(‘IF’).item.json.offset doesnt get updated, i put wait node so that i can stop the execution without infinite loop (for testing purposes). also added Code node to console.log the variables

heres my console log:

[Node: "Code"] 
Object { offset: 1000, total_customer: 1602, batch_size: 1000, offset_from_if_node: 0 }
[Node: "Code"] 
Object { offset: 1000, total_customer: 1602, batch_size: 1000, offset_from_if_node: 0 }
[Node: "Code"] 
Object { offset: 1000, total_customer: 1602, batch_size: 1000, offset_from_if_node: 0 }
[Node: "Code"] 
Object { offset: 1000, total_customer: 1602, batch_size: 1000, offset_from_if_node: 0 }

hopefully its clear, i need to update the offset variable after the Split In Batches node (Done flow), so that i can select the next batch of items and process them.

What is the error message (if any)?

no error message

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.8.2
  • Database (default: SQLite): postgresql
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: MAC OS Ventura 13.5.2

hello @slenderwrist

can you provide the samples for SelectTotalCustomer (e.g. 10 items) and SelectPaged( e.g. 2 iterations with 10 items each) nodes ?

i’ve pinned data for both of the microsoft sql nodes, SelectTotalCustomer returns

[
  {
    "total_customer": 1602
  }
]

SelectPaged returns

[
  {
    "name": "First item",
    "code": 1
  }
]

console.log output in the question, is provided when execution is run with the above given pinned data.

Sorry, missed the answer.

I think the issue is that you can’t refer to IF.item property, as there is Split node and msql node after the IF node, that will mess with the item reference. Instead, try to refer with IF.last() item, as it will be technically the last item in the moment.

But there might be an issue with Split in batches node, so it might not working as you are expecting :slight_smile:
If that happens, you will need to move the Split functionality to the sub-workflow and execute the sub-workflow itself.

1 Like

accepted solution did the trick. My actual workflow also does stuff in the ‘SplitInBatches’ nodes loop flow. Like you mentioned, not sure about the ‘SplitInBatches’ node loop flow.

I needed a fast solution on this one and i had access to redis, so i used redis nodes get/set operation as a temporary variable storage, not optimal but it worked, just leaving here if someone else faces a similiar problem.

tnx

1 Like

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