Switch node to more than one path

I have configured a switch node in a workflow with regex. If a value is met twice or three times, it should go all the necessary paths.
Unfortunately in my example it is runnig only to path0 for the example value of “Tarif” = “BP700”.

For this example value it should go through outputKey0 and outputKey1. How can I achieve that?

Thank you!

switch node config

    {
      "parameters": {
        "dataType": "string",
        "value1": "={{ $('Merge').item.json.rawRequest.Tarif }}",
        "rules": {
          "rules": [
            {
              "operation": "regex",
              "value2": "BP(300|700|900|1100)",
              "outputKey": "0"
            },
            {
              "operation": "regex",
              "value2": "BP(400|700|900|1100)",
              "outputKey": "1"
            },
            {
              "value2": "BP250",
              "outputKey": "2"
            },
            {
              "operation": "regex",
              "value2": "BP(100|200|900|1100)",
              "outputKey": "3"
            }
          ]
        }
      },
      "id": "21281a8a-71ee-4340-90d5-5bd1bc6bd95b",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [
        1020,
        1060
      ]
    }

Information on your n8n setup

  • n8n version: 1.15.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Ubuntu 22.04

Hey @mokkin,

It seems like the switch node acts as if you would add a break; statement after each case in js, and thus only ever returns an output at the first match of your regex. You can see the same behavior with this simple test setup:

I think this is probably caused by line 688 here: https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Switch/V2/SwitchV2.node.ts

It would be cool if this option would be configurable and you could choose if the switch node is able to assign an item to multiple outputs.

I think your best option at the moment, is just to use 4 "IF-Nodes" (if you only have the four options you showed in your switch node config).

This way you would evaluate every option individually and bypass this problem.

I hope that helps :slight_smile:

3 Likes

Hi folks, @one_juru is spot on here.

Instead of the IF node you could also consider using Filter nodes. They are almost identical to the IF node but have only one output, so will keep the workflow just a little bit cleaner.

1 Like

Hi, I don’t undertand how the filter node could help in this case. Could you give us an example?

Hi @lpadula
this topic is not 100% valid anymore as the new Switch node allows to:

  1. Have as many outputs as you need
  2. The option “Send data to all matching outputs” allows you to route items to all matching routes

so all the issues described here are solved by the new Switch node released in 1.24.0.
Best

1 Like

Hi!

The issue lies in its “break” logic, which restricts the ability to match multiple criteria simultaneously. For instance, consider an input scenario where the string includes various roles such as “administrator, developer, maintainer”. Ideally, I want the system to recognize and assign each of these roles appropriately. However, due to the existing logic, it processes only the first matching criterion and disregards the rest.

Hi @lpadula,
check this workflow:

The item is role: "administrator, developer, maintainer",
the 3 rules in the Switch node are checking for the 3 strings: administrator, developer, and maintainer.
If Send data to all matching outputs is ON, the item is sent to all 3 outputs.
is this what you need?

Let me know
Best

Yes, that’s perfect! Thank you very much!

1 Like

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