Accepting input from any node

Hi,

my question is fairly simple. Often it happens, that I have to prepare data based on a condition, maybe load some additional data or just do something with the already existing output. This means I split the workflow path into two branches. After the work is done I want to merge back into the original branch. So I can continue and don’t have to copy nodes that basically do the same job.
This is a simple example of what I am trying to do:

This does not work however. I guess it is because the IF node returns either true or false. For example, the IF node returned true, the data was successfully prepared but the last node won’t execute because the IF node returned true and since the sheets node is linked to the false output, it won’t execute.

I believe this is expected behavior and I was naive to believe it would work that way. But is there a similar pattern? I do not want to duplicate nodes and/or whole branches just because I have to occasionally do some extra work before continuing.

Any tipp would be greatly appreciated :slight_smile:

Hey @itd24 :wave:

Welcome to the community!

There are two possible solutions that I can think of based on the information that you have provided.

Solution 1:
Instead of using the IF node to check the condition, I would do that in the Function node itself, since we are already using it to manipulate data. Based on whether the condition is true or false, I would design my function to give me the desired output.

Solution 2:
You can use the Merge node to merge data. However, I am not sure about this solution. I haven’t tested it myself.

Let me know if this helps :slight_smile:

1 Like

Thank you for your answer. The first option would not work since this was only a simple example. Another example would be:
Load data from a spreadsheet -> if there is no data -> load data from api -> merge back into the main branch and do the exactly same thing with the data regardless of it came from the api or the spreadsheet.
I will try the merge node and if it does not work I’ll try to build a node similar to the merge node that does the trick. Thanks :grin:

1 Like

Perhaps the “Execute workflow” node could help you here? It allows for you to run a different workflow as a node. Has also the added benefit of a single source of truth for a process that you use in multiple places.

It has a few different modes, so for example can even run a workflow simply from WF JSON pasted into the node’s parameters.

1 Like

This may be a solution, thank you. I’ll look further into it. :slight_smile:
I’ll solve the problem, one way or another :smiley:

So, the merge node does not work. It does not matter what the output of a node is, if the second one linked to the merge node does not get executed the merge node does not output any data.
Running different workflows won’t do any good since I can not send any data to the workflow I want to execute. Besides, having too many partial workflows can quickly become messy.

After thinking about this problem I may have found a solution. In the last few years I worked with microservices, and thus I might have had a completely wrong approach to this problem. I was treating the nodes as microservices. The funny thing is that may be a possible solution.

The idea is that every branch would save the resulting data into a table in the Postgres database. A “sub-workflow” (let’s call them sub workflows although I see it more as a sub-procedure) would be linked to a cron node. The cron node would be executed every few seconds and load all entries in the Postgres database. The entries would then be processed.

It may be a little overkill, but it would get the job done.

Btw, I am absolutely thrilled about n8n and although it may not have been meant for complicated workflows it has the potential to do exactly that :smiley:

Sorry I am totally confused. Why does the workflow from your first post not work? I can not think of a reason why it could cause problems. The node “Google Sheet1” should be executed no matter if the IF-Node has data in true or false. Can you please post an example workflow that shows the problem you have.

Hi,
sory for the late response :slight_smile: ,
the workflow from my first post did not work. The IF node either triggered “true” or “false”. So if, for example, the IF node triggered “true”, there was no data on the “false” branch and vice versa. And if there was no data at one of the branches, the google sheets node did not trigger. To cut it short, the Google Sheets node never trigerred.
I did not look at the n8n source code so I can not tell you why, but I believe n8n is built that way and this is not a bug.

hm as I wrote n8n I am pretty sure it is not built that way.

It is correct, it should either send data to the “true” or “false” branch (maybe even both if you have multiple items) but because the Sheet node is connected to both, it should always run. If it does not do that, it would be a bug.

1 Like

I assumed it works with promises and that if not both were resolved/rejected it would not trigger. I can try it again if you want, maybe I did not wire it up correctly? All I know is that on the first couple of tries it did not work so I adapted another strategy

Honestly not sure what you mean with promises, did you have them in the Function-Nodes?

Here are an example workflow that seems to work similarly to the one you had above:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      a: 1\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        700
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"a\"]}}",
              "operation": "equal"
            }
          ]
        }
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        700
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.b = 0;\nreturn items;"
      },
      "name": "Function2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        600
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1050,
        700
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [
          {
            "node": "Function2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function2": {
      "main": [
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

There it resolves fine. If you think that the problem you had is a bug, then it would be great to get a reproducible case to be able to debug and fix it.