Ir para o conteúdo

Lição 13 · Flow Control

Branching with If, Switch and Filter

Route items in n8n with If, Switch and Filter, combine conditions, handle missing values and unmatched cases, and keep branching logic clear and testable.

Real processes have rules: large orders need approval, support tickets go to different teams, and test records should be ignored. Three nodes handle most decisions. They look similar, but they behave differently.

Three decision nodes

If True or false paths
Switch Several named paths
Filter Keep or drop items
1 / 2

How the conditions work

These nodes check each item separately. If you send ten items to an If node, some can go to true and others to false in the same execution. Both paths then run, each with its own items.

A condition compares a value with another, such as is equal to, contains, is greater than or exists. Select the right data type for the comparison. A text value such as "100" compared as a number may need conversion first.

If

Use If when there are two outcomes:

TEXT
Value 1  : {{ $json.total }}
Operator : Number, is greater than or equal to
Value 2  : 500

You can add several conditions and combine them with AND (all must pass) or OR (any may pass). For mixed logic such as "(A and B) or C", it is often clearer to compute a field first with Edit Fields, then test that field.

Switch

Use Switch when there are several outcomes, such as routing tickets by category. Each rule has its own output. Check these settings:

  • What happens to unmatched items? Add a fallback output so nothing disappears unnoticed.
  • Can an item match several rules? There is an option to send items to all matching outputs rather than only the first.
  • Rule order matters when only the first match counts. Put specific rules before general ones.

Filter

Filter keeps items that pass and discards the rest. Use it for "ignore anything marked test" or "keep only overdue invoices". If you need the rejected items later, use If instead.

Design the rules first

It helps to write decisions as a table before you build:

Condition Route
total >= 500 Needs approval
country is empty Fix data
anything else Process
Python Stand-in: route orders like a Switch node

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice A4: its total is text. Without float(...) the comparison would fail in Python. In n8n, choose the right type in the condition.

Test the edges

Check boundaries: exactly 500, a missing field, an empty string, a surprising capital letter such as "URGENT". Normalize text before comparing:

TEXT
{{ ($json.priority ?? '').trim().toLowerCase() }}
JS Stand-in: normalize before comparing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Empty branches

If no items go down a branch, its nodes normally do not run. If a later step must happen regardless, design for it: for instance, use a separate path that always has data, or the Always Output Data setting, used carefully. Remember that in 3.0, If and Switch add the empty item only when every output is empty.

Common mistakes

  • Forgetting a fallback, so unmatched items vanish.
  • Comparing numbers stored as text.
  • Building a maze of nested If nodes where one Switch or a calculated field would be clearer.

Worked example: ticket routing

Suppose tickets arrive with priority, plan and subject. The business rules are: urgent tickets from paying customers go to the on-call channel; other urgent tickets go to the urgent queue; billing subjects go to finance; everything else goes to the main queue. Write them in that order, because the first match should win. Then compute helper fields with Edit Fields, such as isUrgent and isPaying, so each Switch rule stays short and readable.

Now list test tickets that exercise every route, including one that matches two rules, such as an urgent billing question. Ask the person who owns the process which route it should take. Rules that conflict are a business decision, not a technical one. Write the decision in a sticky note next to the Switch node so that nobody reorders the rules by accident later.

Finally, count items per route in your first production runs. If the fallback receives much more than expected, your rules do not match the real data, for example because the source uses P1 where you expected urgent.

Practice

Write a rule table for sorting job applications into three routes without using any personal characteristic that would be unfair or unlawful, such as age or gender. Use only criteria the process owner has approved, such as whether required documents are attached. Add a fallback that goes to a person. Then create test items for each route and one that is incomplete. Run them through your logic and compare with the table. Automated decisions about people need extra care, so keep a human in charge of any rejection.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Ten items reach an If node. What can happen?

    Conditions are evaluated per item.
  2. 2. Why add a fallback output to a Switch?

    A fallback captures anything the rules miss.
  3. 3. When is Filter a better choice than If?

    Filter keeps passing items and drops the rest.

Summary

  • If gives two paths, Switch several, and Filter keeps or drops.
  • Each item is evaluated on its own.
  • Plan rules, add fallbacks and test boundary values.
  • Normalize text and use the correct data types.

Next: Merging and Looping

Seu progresso fica salvo neste navegador. Não precisa de conta.