Loop does not loop if the return branch exists

In multiple workflows, when I close the loop, the loop stops after one iteration.

This workflow runs just fine. I get valid results after the first run. Of course, since the loop is not complete, the workflow then stops.

However, if I close the loop, the workflow stops in the middle of the loop. The workflow ends, and nothing productive is done.

(My message is too long. I will continue this message in the first comment. )

Information on your n8n setup

  • n8n version: 1.58.2
  • Database (default: SQLite): PostgreQL 15
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker on Digital Ocean
  • Operating system: Ubuntu 22.04 LTS

(Continuing my message)

Specifically, it stops at the larger Merge node. Here is an image:

No error messages

Share the output returned by the last node

I expect the workflow to return a JSON object containing updates that need to be made to a Google contact. Once I have all the objects, I will send them to Google in an API call.

I just discovered that if I embed the workflow call in another loop, the loop does execute. It’s not running any of the nodes that don’t get run with the closed loop above, but all the data is being processed…

Here’s what I mean. In workflow #1, I have an “Execute a workflow” node:

I format the data before calling workflow #2.

When I configure a loop in workflow #1, or workflow #2, The workflow will stop at the larger merge node in workflow #2.

If I configure a loop in both workflow #2 and #2, then the second loop will loop the workflow.

I’m not sure if this gives any insight into why the workflow stops, but it’s another data point.

-Russ

Hey @russellkg,

For the loop to work it needs to loop back to the start but every node needs to output data otherwise the loop will stop so I suspect in this case one of the nodes is not returning the data needed and that is causing it to stop.

You can work around this by setting the node to always output data under the settings but this does also mean you can get empty data so you will need to make sure you use if nodes to make sure you have data to process.

Hi Jon,

That’s the thing, in my example, I’m sending the exact same data through the workflow.

When the return path to the beginning of the loop exists, the flow stops, on the first run, at the larger of the two merge nodes.

When the return path does not exist, the flow continues to the end, as if it would loop were the path there.

Again, this is the exact same data, pinned in the manual start node.

-Russ

Hey @russellkg,

A merge node typically needs all of the inputs to have data before it will output, Looking at your earlier screenshot you can see where some of the set nodes don’t have data so the big merge only half runs which means the next merge doesn’t get the input it needs to continue.

1 Like

OK, then I have other questions.

I have been using merge nodes in this way in several workflows. In this flow specifically, I have to compare the contact card fields in Google Contacts with our master records in HaloPSA.

  1. To do so, I break down the fields so I have the same list from each platform. (Client, ClientID, Firstname, Lastname, EmailAddress, WorkPhone, WorkCell, HomeCell)
  2. Field comparison in the switch node
    1. If the fields matche, nothing further is done with that field.
    2. If HaloFirstname != GoogleFirstname, go down this path.
  3. Set the update variable
  4. Merge the data that comes out.
  5. Make the updated JSON to send to Google.
  6. Send it.
  7. Loop to the next record.

Now you are telling me this shouldn’t work. So my question is, how should I have built this?

I think the switch node is right, but I’m going to get all permutations of fields to update. How should I be building the JSON to send to Google?

Thanks!

-Russ

Trying not to belabor the issue. :slight_smile:

In this thread, @JayF seems to be saying to use the merge node as I’ve outlined.

Loop basic functionality

Thank you!

-Russ

Hey @russellkg,

It depends on the the option being used in the merge node but looking at your workflow we can see it is stopping because there is no input it looks like the switch is using always output data so it is unusual that it has not outputted any data which would have worked around the problem as all of your outputs should have something so there could be an issue there.

Can you share some example input data that I can use for testing?

It does not stop on the first contact when the loop returns do not exist.

It does stop on the first contact when the loop returns exist.

This is inconsistent behavior. The same data should not exhibit different behavior when the only change is connecting the end node to the loop node.

If this is not how I should be constructing this type of a flow, how should I be doing so? Can you give me an example of a workflow like this?

-Russ

I’m not sure what to do here.

The fact remains that I have a loop that will complete a single run if the end node is not connected to the start node of the loop. If I connect the end node to the start of the loop, the loop terminates in the middle.

This is inconsistent behavior. The data being processed is the same.

It was inferred that I’m doing this wrong. I’m told that merge nodes need every input to pass data before they trigger. I am curious, because I’ve been using the fact that they don’t all need to have data, particularly with data that may or may not have every field that other records have. For example, we have contact records that may have a telephone number in any combination of three fields, or none. I have the merge node accepting input from the three chains that process each phone number. When they finish, the merge node appends one after the other, if more than one exists. I then wrap the resulting JSON in the object they need to get processed by the receiving server. This is how I’m building the JSON body for the web call.

If I’m doing this wrong, I need to know where to get information on how to do it right.

If what I have written above is a valid n8n construct, I’d like to know that as well.

Help!

-Russ

I’m still having this issue.

On several workflows, when I build a flow with data to loop through, if I close the loop, the flow stops in the middle of the flow. However, if I don’t close the loop, the flow runs to the end of the first contact without issue.

I need to know if I’m I doing this wrong, and if so, how should I be doing this.

I’m left hanging here in the lurch at this time.

-Russ

I have an 18 day old thread that needs some love.

I’m having trouble with looping and/or merging in the referenced thread. I kinda feel like I’m being hung out to dry now, as the last reply from anyone was 9 days ago. I need some closure on this.

No error message. The flow just terminates as described in the referenced thread.

I did share my workflow, on that thread.

There is no output, as the flow ends.

Information on your n8n setup

  • n8n version: 1.60.1
  • Database (default: SQLite): PostgresQL 15
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker on Digital Ocean
  • Operating system: Ubuntu 22.04 LTS

@russellkg opening a new topic is not the way - a reply will also bump it to the top. I merged your posts.

Can you provide a stand-alone, minimal workflow that shows this problem? That’ll help us nail it down.

Probably the issue is with Merge nodes. If it does not receive all items for their inputs, then you may have the situation when the Loop stops.

Why do you need to split all the Edits field nodes instead of using one? (Sorry, didn’t read all thread)

I understand, Bart.

However, I felt I was being ignored after 9 days with zero replies from anyone but me.

Don’t get me wrong. I’ve been doing technical support and IT for over 30 years, starting with anti-virus software on the telephone because we did not yet have the internet. Almost all of it online in some form. Compuserve and AOL before the 'net came along.

In this case, you can see I replied to this thread twice in that 9 days, and not a single reply acknoledging my comment, that someone was looking into it, or anything. I felt I could have bumped it up in perpetuity and not gotten a reply. I felt I needed to do something to get someone elses attention, especially after two message stating “I still have this issue.” How should a person escalate such a situation? I know if I let an issue languish for 9 days with the user blowing in the wind, my boss would (in the vernacular of the 80s and 90s) ream me a new one.

I suspect it’s a bug, but some sort of reply would have been appropriate. I hope you understand the frustration from this side of the keyboard. I will post another reply with the technical stuff.

Here is a simplified version of my flow:

I included 5 records. When you execute that node, it runs all the way to the end without issue. This means the contact being processed is ready to be added to Google.

Now, to see it stop in the middle with the same data, connect a return path from the NOOP node at the end of the flow to the input for the “Create Contact Loop” node. Run the workflow with this one change, and it stops at the HaloPSA Merge Numbers node.

Thanks for looking into this.

-Russ

That happens because the Merge node does not receive data for all inputs.

I see two options how to mitigate this:

  1. Instead of the Switch node with all edits nodes, use one Edit fields node and set the values for fields if they are present, or set some empty values, if absent
  2. Use code node to manually build the JSON as it’s quite easier to do it there (I can help with that a little bit later)
1 Like

The example with the Code node is below

1 Like

If you want the no-code approach, that’s how you can achieve that

1 Like