All right! In this tutorial, you will learn how to implement branching logic in n8n workflows to automate complex business processes involving multiple decision paths. Using a practical example of order status management, you will discover how to fetch data from Google Sheets, apply branching with the Switch node, and send notifications via email and Slack — all without writing any code.
This lesson will deepen your understanding of n8n’s powerful branching capabilities to create smart automations that handle multiple scenarios efficiently. For more on how to access and manipulate data within nodes, check out our guide on Expressions in n8n.
Understanding the Business Scenario
Imagine a business where an operator manually reviews orders in various states: Pending, Processing, Canceled, and Refunded. Based on the order status, the operator performs different actions:
- Pending orders: Alert the customer service team via email to follow up with the customer.
- Processing orders: Notify the operations team via Slack for expedited shipping (especially for high-priority orders).
- Canceled or refunded orders: Send an official email to the finance team and notify them via Slack, especially during financial cycles like month-end.
Manual handling of this workflow is time-consuming and error-prone, often taking hours daily. Automating this process in n8n can save time, reduce errors, and ensure timely notifications. Understanding how to link related data items can also enhance your workflow’s efficiency; see our lesson on n8n Item Linking Explained for more details.
Step 1: Create a New Workflow and Add a Trigger
- Log in to your n8n cloud instance or local workspace.
- Click Create Workflow to open a blank canvas.
- Add a trigger node to start the workflow. For testing purposes, use the
Manual Triggernode. - In a real-world scenario, replace this with a
Schedule Triggernode to run the workflow automatically every day at a specific time (e.g., 9:00 AM).
Step 2: Fetch Order Data from Google Sheets
The order data is stored in a Google Sheet with columns like Order ID, First Name, Last Name, Customer Email, Order Status, and Order Date.
- Add a
Google Sheetsnode. - Configure the node:
- Resource: Sheet
- Operation: Get Rows
- Authentication: Use your pre-configured Google Sheets credentials.
- Document: Select your Google Sheets document containing the order data.
- Sheet: Choose the specific sheet with the order records.
- Leave filters blank to fetch all rows.
- Click Execute Node or Test Step to fetch the data.
- Verify that the node outputs a list of JSON objects, each representing an order.
Viewing Data in JSON and Table Formats
- Switch between JSON and Table views to inspect the data.
- Each item in the list represents an order with properties like
orderId,orderStatus, etc.
For more details on working with data in n8n, refer to the n8n Data Documentation{:target="_blank"}.
Step 3: Add a Switch Node for Branching Logic
Since you need to handle multiple order statuses differently, you’ll use the Switch node instead of multiple If nodes.
Add a
Switchnode connected to the Google Sheets node.Configure the
Switchnode:Property to Evaluate: Use an expression to get the order status property:
{{$json["orderStatus"]}}Rules:
- Add a rule for each status:
pendingprocessingcanceledrefunded
- For each rule, set the comparison to String Equals and enter the status value.
- Add a rule for each status:
Enable Ignore Case to handle variations in letter casing.
Set Fallback Output to ignore items that don’t match any rule (e.g., shipped or delivered).
Rename each output branch for clarity:
- Output 1 → Pending Orders
- Output 2 → Processing Orders
- Output 3 → Canceled Orders
- Output 4 → Refunded Orders
Test the node to see how many items fall into each branch.
Using the Switch node simplifies managing multiple conditions with multiple outputs, avoiding complex nested If nodes. For more on this node, see the Switch Node Documentation{:target="_blank"}.
Step 4: Send Email Notifications for Pending Orders
You want to notify the customer service team about pending orders via email.
Add an Amazon SES node (or a generic
Send Emailnode if you prefer SMTP).Connect the Pending Orders output from the
Switchnode to this email node.Configure the email node:
Operation: Send
From Email: e.g.,
no-reply@yourdomain.comTo Email: Customer service team email (preferably a distribution list)
Subject: Use an expression to include order ID:
Order {{$json["orderId"]}} is in Pending StateBody: Write a message explaining the pending status and any action required.
Execute the node to test sending emails.
Step 5: Send Slack Notifications for Processing Orders
For processing orders, you want to notify the operations team on Slack.
Add a
Slacknode.Connect the Processing Orders output from the
Switchnode to the Slack node.Configure the Slack node:
Resource: Message
Operation: Send
Authentication: Use your configured Slack OAuth token.
Channel: Select the channel used by the operations team (e.g.,
#high-priority-processing-orders).Message Text: Compose a message with order details:
Order {{$json["orderId"]}} is high priority but still in processing state. Please expedite!
Test the node to ensure Slack messages are sent correctly.
Step 6: Notify Finance Team for Canceled and Refunded Orders
Both canceled and refunded orders require notifications via email and Slack.
Step 6.1: Set Up Email Notification
Copy the email node used previously and connect it to both the Canceled Orders and Refunded Orders outputs from the
Switchnode.Adjust the email subject and body to reflect the canceled/refunded status:
Subject:
Refunded/Canceled Order {{$json["orderId"]}} NotificationBody: Include relevant details for the finance team.
Test the node to confirm emails are sent for these orders.
Step 6.2: Set Up Slack Notification
- Copy the Slack node and connect it to both Canceled Orders and Refunded Orders outputs.
- Configure the Slack channel to one used by the finance team (e.g.,
#canceled-refunded-orders). - Compose a simple message indicating the order status and ID.
- Test the Slack node to verify messages appear in the correct channel.
If you need to transform or add additional data before sending notifications, consider using the Set Node in n8n to customize your data payloads.
Step 7: Organize and Test Your Workflow
- Use the Tidy Up option in n8n to neatly arrange your nodes.
- Run the entire workflow manually to verify that:
- Orders are fetched correctly.
- Branching routes orders to the right nodes.
- Emails and Slack messages are sent as expected.
Understanding Branching in n8n
As demonstrated, n8n supports two types of branching:
- Exclusive branching: Each item follows one path based on conditions (e.g., the
Switchnode routing orders based on status). - Parallel branching: An item can be sent to multiple paths simultaneously (e.g., canceled orders triggering both email and Slack notifications).
Using the Switch node simplifies managing multiple conditions with multiple outputs, avoiding complex nested If nodes.
Common Mistakes & Troubleshooting
- Not renaming output branches: This leads to confusion when debugging. Always rename outputs in the
Switchnode for clarity. - Incorrect expression syntax: Use the expression editor to drag and drop properties instead of typing manually to avoid syntax errors.
- Case sensitivity issues: Always enable Ignore Case in the
Switchnode to handle inconsistent data formats. - Slack or email credentials misconfigured: Ensure your Slack OAuth token and email SMTP/SES credentials are set up correctly before running tests.
- Sending too many messages at once: Slack and email providers may rate-limit you. Consider batching or adding delays if processing large volumes.
Useful n8n Documentation Links
- Switch Node Documentation{:target="_blank"}
- Google Sheets Node Documentation{:target="_blank"}
- Slack Node Documentation{:target="_blank"}
- Email Node Documentation{:target="_blank"}
Quick Reference Cheat Sheet
| Step | Node Type | Key Settings |
|---|---|---|
| Trigger | Manual / Schedule Trigger | Set schedule or manual execution |
| Fetch Orders | Google Sheets | Operation: Get Rows, select sheet and document |
| Branch by Order Status | Switch | Property: {{$json["orderStatus"]}}, add rules for each status, enable Ignore Case |
| Notify Customer Service | Amazon SES / Email | To: Service team, Subject/body with order info |
| Notify Operations Team | Slack | Channel: Processing orders, Message with order ID |
| Notify Finance Team (Email and Slack) | Email & Slack | Channels and emails for finance, message with order ID |
By following this tutorial, you can efficiently automate complex workflows with multiple decision paths in n8n, saving time and reducing manual errors. As shown in the video above, leveraging the Switch node for branching is a powerful method to handle diverse business scenarios seamlessly.