Split in batches returns less items than expected

Please check this demo example. if the first hackernews returns 2 items and than 2nd hackernews return 2 for each I would expect the set modules after the last split in batches to run 4 times each, but it only runs with the first split in batches run. I cannot figure out why. Any ideea?

Share the output returned by the last node

I am on n8n cloud.

Hi @Tbiyss, it seems you are using two overlapping loops here which would cause the problem.

Once you re-arrange your workflow this should function as expected:

Your Set nodes will run 4 times each as expected here:

i was using the hacker news for a mockup here.
Think about the first loop as searching for contacts, and than the second hacker news - searching for invoices for each contact.

First loop takes each contact 1 by 1, than I search for all invoices related to contact 1, than I have the second loop for each invoice related to contact 1.

Practically interating all contacts, getting invoices related to the contact and interating all invocies and applying some logic.

I would expect
Search contacts to return 2 items:
Loop 1-1: Search invoices for Contact1 returns 2 items. Loop 2-1 for 1: Returns 3: 3 items (2 invoices + Done)
Loop 1-2: Second Item Search for Contact2 invoices, Loop 2-2 for 2: returns 3: 3 (2 invoices + done)

What am I getting wrong here?

You’re mixing loops here which won’t work the way you want it too. So I’d suggest you implement your loop 2 fetching your invoices as a sub-workflow.

Have your main workflow (loop 1) through your contacts, then call the sub-workflow through the Execute Workflow loop. This will also result in improved readability of your workflow, for example:

I understand what you are saying however, are you telling me there is no way to run 1 loop inside another loop?

For(contact of contacts array) {
Searh Invoices for contact,
For (invoice of invoices array)
{‘lot of operations’}

I understand what you are saying however, are you telling me there is no way to run 1 loop inside another loop?

Using a sub-workflow you can do exactly that and this is the way I’d strongly recommend using. It will keep workflows shorter and thus easier to read and manage.

However, you can also do it without a sub-workflow if you absolutely want. Considering that loops are often used to process larger amounts of data and n8n cloud instances have very limited memory, this is an approach I’d usually avoid to reduce the chance of crashing an n8n cloud instance (using a sub-workflow you can control which data is returned to the parent workflow workflow after each execution, without the sub-workflow all data from your inner loop would be kept in memory for the entire duration of the parent workflow execution).

If you still want to go for it, make sure to upgrade to [email protected] to make sure you have access to the data required for such a construct.

1 Like

Thanks for your answer! For some reasons I don’t see 0.234 in versions.any ideea why?

Ah, 0.236 is also fine, it just has to be 0.234 or later. So you should be all good here :smiley:

1 Like

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