Skip to content
Stand with Ukraine flag

Switch

Use this node when you need to route a message to one or more named connections based on custom logic — for example, routing to High, Normal, or Low based on a sensor reading, or fanning a single message out to multiple parallel paths simultaneously. The script returns a connection name or an array of names.

  • Script language — required. Choose TBEL or JavaScript.
  • Script body — required. A function that receives three arguments and must return either a string or an array of strings (each string is a connection label):
    • msg — the message data (object or array).
    • metadata — the message metadata. In JavaScript, an object where all values are strings; in TBEL, a java.util.Map<String, String>.
    • msgType — the message type as a string.
  1. Select the script based on the configured Script language.
  2. Execute the script function.
  3. Interpret the return value:
    • A string → treated as a one-element list; route the message via that connection.
    • An array of strings → route the message via each listed connection.
    • Any other type, or an exception → route via Failure.
ConnectionCondition
(any name returned by the script)The message is routed via every connection name the script returns.
FailureThe script returned an invalid type (e.g., number, object) or threw an exception.
{
"scriptLang": "TBEL",
"tbelScript": "return 'A';"
}

Result: routed via A — a string is treated as a one-element list.


{
"scriptLang": "TBEL",
"tbelScript": "return ['A'];"
}

Result: routed via A.


Example 3 — Multiple connections → A, B, C

Section titled “Example 3 — Multiple connections → A, B, C”
{
"scriptLang": "TBEL",
"tbelScript": "return ['A', 'B', 'C'];"
}

Result: the message is forwarded to all three connections simultaneously.


Example 4 — Dynamic routing by temperature → High

Section titled “Example 4 — Dynamic routing by temperature → High”

Incoming message

Data:

{
"temperature": 85
}

Node configuration

{
"scriptLang": "TBEL",
"tbelScript": "var t = msg.temperature;\nif (t > 80) return 'High';\nif (t > 60) return 'Normal';\nreturn 'Low';"
}

Result: routed via High — temperature (85) exceeds 80.

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TbJsSwitchNodeConfiguration",
"type": "object",
"required": ["scriptLang"],
"properties": {
"scriptLang": {
"type": "string",
"description": "Scripting language used to execute the function.",
"enum": ["TBEL", "JS"]
},
"jsScript": {
"type": "string",
"description": "JavaScript function body that must return a string or array of strings. Used when 'scriptLang' is 'JS'."
},
"tbelScript": {
"type": "string",
"description": "TBEL function body that must return a string or array of strings. Used when 'scriptLang' is 'TBEL'."
}
},
"additionalProperties": false
}