Skip to main content

Switch

The Switch node enables the flow to proceed from the output ports for which the provided JavaScript expressions evaluate to true.

Switch

info

If you assign a value of true to the bottom Output port, it can serve as a default when none of the preceding evaluations return true. However, remember to enable the Break option. If it's left unchecked, the flow will proceed from all ports that are evaluated as true.

How It Works

  1. The node receives a message and evaluates each configured output port condition in order
  2. For each condition:
    • If the condition is empty, it evaluates to false
    • If non-empty, the JavaScript expression is evaluated with access to msg, global.get(), and flow.get()
    • The expression must return a boolean value (true or false)
  3. If Break is enabled:
    • The node stops evaluating conditions after the first true result
    • All remaining conditions are set to false
    • Only one output port will send the message
  4. If Break is disabled:
    • All conditions are evaluated regardless of previous results
    • The message is sent to all output ports where the condition evaluates to true
  5. If no conditions evaluate to true, an error is raised
  6. The message passes through unchanged to the selected output port(s)

Requirements

  • At least one output port condition must be configured
  • All conditions must be valid JavaScript expressions
  • Expressions must evaluate to boolean values (true or false)
  • At least one condition must evaluate to true, or an error will occur
  • Global/Flow variables accessed in conditions must exist

Error Handling

Error CodeDescriptionCause
Core.Programming.Switch.ErrOnCreateConfig parse errorInvalid node configuration during creation
Core.Triggers.Inject.OnMessageMessage parse errorInvalid message format received
Core.Programming.Switch.NoConditionNo conditions configuredThe Output Ports array is empty
Core.Programming.Switch.InvalidConditionInvalid conditionCondition expression cannot be evaluated or variable not found
Core.Programming.Switch.EmptyConditionEmpty condition at position NThe Nth condition field is blank
Programming.Switch.EvalJavaScript evaluation errorSyntax error in one of the condition expressions
Programming.Switch.Eval.ValueValue retrieval errorCannot retrieve evaluated results
Programming.Switch.Eval.ArrayArray export errorCannot convert evaluation results to array
Programming.Switch.ErrExprNon-boolean expressionCondition expression returned a non-boolean value
Core.Programming.Switch.ErrConditionsNo matching conditionAll conditions evaluated to false

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if the Catch node is used.

Options

  • Break: Allows the flow to exit the loop or switch prematurely when any output port's evaluation returns true. If this option is not selected, the flow can proceed from multiple outputs that evaluate to true.

  • Output Ports: This option requires an array of expressions, one for each output port. Input a JavaScript expression corresponding to each port. The provided expression will be evaluated using the input value.

JavaScript Expression API

Within your condition expressions, you have access to:

  • msg - The current message object (e.g., msg.status === "active")
  • global.get(variableName) - Retrieve a global variable value
  • flow.get(variableName) - Retrieve a flow variable value
  • Standard JavaScript operators and functions

Usage Examples

Example 1: Route by Order Status

Configure Switch with 3 outputs:
- Output 1: msg.status === "pending"
- Output 2: msg.status === "completed"
- Output 3: true (default/else)
Break: enabled

Flow:
Get Order
└─ Switch
├─ [Port 1] Process Pending Order
├─ [Port 2] Archive Completed Order
└─ [Port 3] Handle Unknown Status

Route orders to different processing paths based on their status.

Example 2: Multiple Condition Routing (No Break)

Configure Switch with 3 outputs:
- Output 1: msg.amount > 1000
- Output 2: msg.priority === "urgent"
- Output 3: msg.customer.vip === true
Break: disabled

Flow:
Process Order
└─ Switch
├─ [Port 1] Send to Manager Approval
├─ [Port 2] Add to Urgent Queue
└─ [Port 3] Notify VIP Team

An order can trigger multiple outputs simultaneously (e.g., high-value urgent VIP order triggers all three).

Example 3: Numeric Range Routing

Configure Switch with 4 outputs:
- Output 1: msg.score >= 90
- Output 2: msg.score >= 70 && msg.score < 90
- Output 3: msg.score >= 50 && msg.score < 70
- Output 4: msg.score < 50
Break: enabled

Flow:
Calculate Score
└─ Switch
├─ [Port 1] Excellent - Send Reward
├─ [Port 2] Good - Send Encouragement
├─ [Port 3] Average - Send Tips
└─ [Port 4] Poor - Send Training Materials

Route based on numeric ranges with Break enabled to ensure only one path is taken.

Usage Notes

  • Conditions are evaluated in order from top to bottom (Output 1, then Output 2, etc.)
  • With Break enabled, only the first matching condition triggers - ideal for if/else-if/else logic
  • Without Break, multiple outputs can fire - useful for triggering multiple parallel actions
  • Use true as the last condition for a default/"else" case (works best with Break enabled)
  • The message object can be modified within expressions, but changes won't persist
  • Empty/undefined fields in msg evaluate to falsy values in JavaScript
  • Non-boolean expressions (returning strings, numbers, objects) will cause errors

Tips

  • Use Break mode for mutually exclusive routing (status checks, priority levels)
  • Disable Break when one input should trigger multiple parallel processes
  • Place most specific conditions first, generic conditions last
  • Use the last output port with condition true as a catch-all/default route
  • Test complex expressions in a Function node first before using in Switch
  • Access nested object properties safely: msg.user && msg.user.role === "admin"
  • Combine conditions with logical operators: msg.age 1 or greater8 && msg.verified === true
  • Use global/flow variables for dynamic routing: msg.priority === flow.get("currentPriority")
  • Function - More complex conditional logic with full JavaScript
  • For Each - Loop through items before routing
  • Break - Exit from loops based on conditions
  • Debug - Inspect message data before switching