Overview
In this tutorial, you will learn how to master conditional logic in n8n using the If node, understand execution order, and implement branching strategies to build intelligent, adaptable workflows. By the end, you'll be able to create workflows that handle different scenarios dynamically, manage multiple branches efficiently, and combine data streams seamlessly using the Merge node.
Understanding Conditional Logic in n8n
Conditional logic is the backbone of smart workflows. It lets you ask questions like "If this condition is true, do X; otherwise, do Y." This approach is essential for workflows that need to handle different data inputs or outcomes dynamically.
Key Concepts of Conditional Logic
- If-Else Condition: The most common form of conditional logic where the workflow splits into two branches — one for
true(if the condition is met) and another forfalse. - Examples:
- If a ticket is marked urgent, notify the manager; else, assign normally.
- If a customer opens an email, send a follow-up; else, add them to a different sequence.
- If a lead is from a high-priority industry, fast-track them; else, assign to the normal pipeline.
Implementing If-Else Logic with the If Node
The If node in n8n allows you to define one or more conditions that split the workflow into two branches:
- True Branch: Executes when the condition(s) are met.
- False Branch: Executes when the condition(s) are not met.
Example Conditions in an If Node
{
"conditions": {
"all": [
{
"field": "companyName",
"operation": "isNotEmpty"
},
{
"field": "email",
"operation": "doesNotEndWith",
"value": "gmail.com"
},
{
"field": "email",
"operation": "doesNotEndWith",
"value": "hotmail.com"
}
]
}
}
These conditions check that the company name exists and the email domain is neither Gmail nor Hotmail.
To better understand how data flows and transforms through nodes like If, consider reviewing Understanding Data in n8n, which explains how data is structured and passed between nodes.
Using the Filter Node as an Alternative
The Filter node is another way to apply conditional logic but differs because it does not output multiple branches. Instead, it filters out items that do not meet the condition, passing only the matching items downstream.
- Kept Branch: Items that meet the condition.
- Discarded Branch: Items that do not meet the condition (not forwarded).
When to Use Filter vs If
| Feature | If Node |
Filter Node |
|---|---|---|
| Outputs multiple branches | Yes (True and False branches) | No (only one output branch) |
| Use case | When you need to perform different actions based on condition | When you want to filter data and only continue with matching items |
| Workflow complexity | Higher (due to branching) | Lower |
Practical Steps to Configure an If Node
- Drag the
Ifnode onto the canvas. - Connect the previous node (e.g., Google Sheets) to the
Ifnode. - Define your conditions under the
Conditionssection. - Connect the
Trueoutput to nodes that handle the true scenario (e.g., send Slack message). - Connect the
Falseoutput to nodes that handle the false scenario (e.g., update Google Sheets for unqualified leads).
For more details on configuring nodes that manipulate data, the official n8n Set node documentation provides useful insights, especially since expressions often complement conditional logic.
Branching in n8n: Types and Use Cases
Branching allows workflows to split into multiple paths based on conditions, enabling complex decision-making and parallel processing.
Types of Branching
- Conditional Branching (
IfNode): Splits workflow into two branches based on a boolean condition. - Multi-Path Branching (
SwitchNode): Splits workflow into multiple branches (n branches) based on matching conditions. - Parallel Branching: Executes multiple actions simultaneously from the same node output.
Multi-Path Branching with the Switch Node
The Switch node evaluates a single field against multiple values and routes the workflow into one or more branches accordingly.
Example: Branching Based on Order Status
| Condition | Branch Output Name |
|---|---|
| orderStatus == "pending" | Pending Orders |
| orderStatus == "processing" | Processing Orders |
| orderStatus == "cancelled" | Cancelled Orders |
| orderStatus == "refunded" | Refunded Orders |
How to Configure the Switch Node
- Add the
Switchnode after your data source (e.g., order data). - Select the field to evaluate (e.g.,
orderStatus). - Add multiple values and assign branch names for each condition.
- Connect each branch to nodes performing the respective actions.
Parallel Branching
Parallel branching occurs when you connect multiple nodes directly from a single output, allowing simultaneous actions.
Example
- From the
Cancelled Ordersbranch, you send an email and send a Slack message concurrently. - From the
Refunded Ordersbranch, you perform the same two actions in parallel.
Execution Order in n8n: How Branches Run
Understanding execution order is critical to avoid workflow errors, especially when branches depend on each other.
Default Execution Rules
- Branch Execution Happens Sequentially: One branch completes before the next begins.
- Top to Bottom Execution: Nodes higher on the canvas execute before nodes lower down.
- Left to Right Execution for Nodes at the Same Height: Leftmost nodes execute before rightmost nodes.
Why Execution Order Matters
If a node in one branch depends on data from another branch that runs later, your workflow will fail or produce unexpected results. Properly ordering branches and nodes ensures data dependencies are respected.
Merging Branches with the Merge Node
After splitting workflows into multiple branches, you often need to combine the results back into a single data stream for further processing.
Purpose of the Merge Node
- Waits for all connected branches to complete.
- Combines data from multiple branches into one dataset.
- Supports different merge modes such as "Append", "Merge By Key", etc.
Practical Example: Merging Order Data
Suppose you have two data sources:
- Orders Header: Basic order info like customer name, order date, status.
- Order Details: Items within each order, quantities, prices.
You want to merge these datasets on the common orderID field.
Configuring the Merge Node
- Connect both data sources as inputs to the
Mergenode. - Set the mode to Merge By Key.
- Select the key field to match on (e.g.,
orderID).
This will combine matching rows from both inputs into a single structured dataset.
For a deeper dive into the capabilities and configuration options, check out the Merge Node in n8n tutorial and the official n8n Merge node documentation.
Sample JSON Inputs and Merge Result
Input 1 (Orders Header):
[
{
"orderID": "1001",
"customerName": "Alice",
"orderStatus": "pending"
},
{
"orderID": "1002",
"customerName": "Bob",
"orderStatus": "processing"
}
]
Input 2 (Order Details):
[
{
"orderID": "1001",
"product": "Widget A",
"quantity": 2
},
{
"orderID": "1001",
"product": "Widget B",
"quantity": 1
},
{
"orderID": "1002",
"product": "Widget C",
"quantity": 5
}
]
Merged Output:
[
{
"orderID": "1001",
"customerName": "Alice",
"orderStatus": "pending",
"products": [
{ "product": "Widget A", "quantity": 2 },
{ "product": "Widget B", "quantity": 1 }
]
},
{
"orderID": "1002",
"customerName": "Bob",
"orderStatus": "processing",
"products": [
{ "product": "Widget C", "quantity": 5 }
]
}
]
Step-by-Step: Building a Conditional Workflow with Branching and Merging
1. Set Up the Trigger and Initial Data Source
- Use a trigger node like
WebhookorCronto start the workflow. - Add a data source node such as
Google SheetsorHTTP Requestto fetch relevant data.
2. Add Conditional Logic with the If Node
- Drag the
Ifnode and connect it to the data source. - Define your conditions in the node.
- Connect the
TrueandFalseoutputs to different nodes for respective processing.
3. Implement Multi-Path Branching with the Switch Node
- Add a
Switchnode where multiple conditions need to be evaluated. - Configure values and output branches.
- Connect each branch to the nodes that handle the specific path.
4. Use Parallel Branching for Multiple Actions
- From any branch output, connect multiple nodes to execute parallel actions like sending emails and Slack messages simultaneously.
5. Merge Branches with the Merge Node
- After processing branches, add a
Mergenode. - Connect the branches to the
Mergenode inputs. - Configure the merge mode and key fields as necessary.
6. Continue Workflow with Unified Data
- Use the merged output for further processing, like aggregating data or sending notifications.
For tips on using expressions to dynamically set values in nodes like Set, which often work hand-in-hand with conditional logic, see Expressions in n8n.
Common Mistakes and Troubleshooting
- Not configuring conditions correctly: Ensure your field names and operations in
IforSwitchnodes match the data structure exactly. - Ignoring execution order: Failing to arrange nodes properly can cause branches to execute in the wrong order, breaking data dependencies.
- Forgetting to merge branches: If you split your workflow but don't merge branches that need to be combined, subsequent nodes may not receive the complete data.
- Using
Ifnode whenFilteris sufficient: UseFilternode when you only need to filter data without branching to simplify the workflow. - Mismatched keys in
Mergenode: When merging by key, ensure the key fields exist and are consistent in both inputs.
Useful n8n Documentation Links
Quick Reference Cheat Sheet
| Concept | Node Used | Description |
|---|---|---|
| Conditional Logic | If Node |
Splits workflow into true/false branches |
| Filtering Data | Filter Node |
Filters items passing only matching data forward |
| Multi-Path Branching | Switch Node |
Routes workflow into multiple branches based on values |
| Parallel Branching | Any node output | Multiple nodes connected to same output for parallel actions |
| Merging Branches | Merge Node |
Combines multiple branches into one dataset |
| Execution Order | N/A | Left to right, top to bottom; branches execute sequentially |
By mastering these concepts—conditional logic, branching, execution order, and merging—you'll be able to build robust, flexible, and efficient n8n workflows that can adapt to complex real-world scenarios with ease. As shown in the video above, understanding and using these nodes effectively will elevate your automation skills significantly.