Loop and get results on the end for all items

Hello N8n community,
I have problem to create one Workflow.

I scan some items in background and wait for Success status.
Ussually 90% items are done immediatelly but I wait for scan end of one item.

How I immaginated the Workflow.

  1. Get scan result
  2. If check scan is not success then wait x seccounds and return to 1
    If check scan is OK then go to 3.
  3. Summary one integer field for all items get from scan results.

I fight with it and can’t create the workflow. I tried use SplitInBatches loops, merge nodes and I can’t get working Workflow.

For demonstration I made simple workflow (it uses 2 workflows).

1st workflow - Calculate Risk
2nd for testing poroposes (to make the most reable prototype of my workflow)

Goal:
How should I do this node to calculate risk for all elements if status is success?
On the Calculate risk node I whould have calculation for all 5 items.

I use old N8n version.

Information on your n8n setup

  • n8n version: 0.165.1

How I test:
I run 1st workflow and on 2nd I change status on Function node to success.

1st workflow (main)

2nd workflow

Hi @Dream, perhaps you would want to move the Wait part into your sub-workflow and only return a result once the operation has finished?

So essentially a sub-workflow like below:

And a parent workflow like this:

Provided you use a batch size of 1 to prevent the sub-workflow from returning the results early this should work.

If the main node of the sub-workflow (“Mock random success” in this example, this would obviously be different in your real-world case) doesn’t return a success state, the sub-workflow waits 10 seconds before re-trying the request.

The parent workflow would wait for all sub-workflow executions to finish, then merge the results into a single list again.

I have created this example using a recent version of n8n, so you might want to upgrade your n8n version to run it out of the box. If this is not an option in your case, you would need to backport some of the logic (replacing the “Execute Workflow Trigger” node with a Start node, and the Code node with the older Function node).

Hope this is roughly what you have in mind :slight_smile:

1 Like

Yes! finally works as expected.

The last node “Merge loop items” didn’t work in my version also so I used script from Merge data for multiple executions | n8n workflow template

I spent a lot of time to figure that out… Thank you very much @MutedJam.

1 Like

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