Get startedGet started for free

Building Smarter Workflows with Conditional Logic

1. Building Smarter Workflows with Conditional Logic

Welcome back! In this video, we'll make our workflows smart and adaptive by introducing conditional branches.

2. Branching Workflows

In the real world, processes don't run in a straight line. For example, customers spending more money on a subscription platform may need to be handled and cared for differently from customers spending less. Branching lets a single workflow choose different actions based on the data it sees.

3. If vs. Switch

We've got two main ways to branch in n8n: the "If" node and the "Switch" node. The "If" node maps one value with two possible branches: if the condition is met, or if the condition is not met. We can also stack multiple conditions with AND or OR statements. The Switch node compares the value against several paths, each with their own conditions, and maps it to the one it satisfies. Here's a rule of thumb: if the condition can be translated to a true/false question, use "If"; and if we're choosing one of many categories, use "Switch". If you're unsure, use a Switch node and add a default branch to catch anything unexpected. And if nothing matches, add a Fallback Branch. This is a fallback option, like saying "otherwise, do this".

4. Example: Acme Support Desk

Let's say we're building a workflow for an order management system, and we want to handle orders differently depending on the order details. We'll look at the fields and their values on each order- things like the quantity (is it a bulk order or a regular one) and the category (books, electronics, or other). Based on those values, we can decide how to process it and route each order.

5. Example: Acme Support Desk

To decide whether an order should be treated as bulk, we'll first check the quantity field. If the quantity is greater than 14, we'll mark it as a bulk order for special handling; otherwise, it stays as a regular order. Prioritizing bulk orders ensures larger customers get the right attention, improving efficiency and satisfaction. This can be phrased as a true/false question–is the order quantity greater than 14 or not–so this is a job for the "if" node. We drag the quantity field from the input to value1, adjust the condition to greater than, and input 14 as value2.

6. Example: Acme Support Desk

Next, to determine which team should process the order, we'll check the category field. If it's "Books", it goes to the Books team. If it's "Electronics", it goes to the Electronics team. If it's other, it goes to the General team. Notice how this example wouldn't work well with just an If node, since there are multiple possible categories, the Switch node makes it much easier to handle. We now drag the category field into value1, and set each department name in value 2 with three separate rules. When we go back to the canvas, we can see that the bulk orders will now be sorted by category, but the regular orders aren't. In n8n, we can highlight nodes and copy them easily to avoid rewriting the same logic multiple times. If we execute the workflow, we can see the orders being split by order type and category for careful handling.

7. Let's practice!

Time to give this a go!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.