Skip to content

Switch#

ใช้ Switch node เพื่อกำหนดเส้นทาง workflow ตามเงื่อนไขที่ตั้งไว้ คล้ายกับ IF node แต่รองรับหลายเส้นทาง output

Node parameters#

เลือก Mode ที่ node จะใช้:

  • Rules: เลือก mode นี้เพื่อสร้าง rule สำหรับแต่ละ output
  • Expression: เลือก mode นี้เพื่อเขียน expression เพื่อคืนค่า output index แบบโปรแกรม

การตั้งค่า node จะขึ้นอยู่กับ Mode ที่เลือก

Rules#

ตั้งค่า node ด้วย operation นี้โดยใช้ parameters เหล่านี้:

  • สร้าง Routing Rules เพื่อกำหนดเงื่อนไขเปรียบเทียบ
    • ใช้ dropdown data type เพื่อเลือกชนิดข้อมูลและประเภทการเปรียบเทียบ เช่น ถ้าอยากสร้าง rule สำหรับวันที่หลังจากวันที่ที่กำหนด ให้เลือก Date & Time > is after
    • field และค่าที่ต้องใส่ในเงื่อนไขจะเปลี่ยนไปตาม data type และการเปรียบเทียบที่เลือก ดูรายละเอียดที่ Available data type comparisons สำหรับรายการเปรียบเทียบทั้งหมด
  • Rename Output: เปิด option นี้เพื่อเปลี่ยนชื่อ output field ที่จะใส่ข้อมูลที่ตรงกับเงื่อนไข ใส่ Output Name ที่ต้องการ

เลือก Add Routing Rule เพื่อเพิ่ม rule เพิ่มเติม

Rule options#

ตั้งค่าเพิ่มเติมได้ด้วย Options เหล่านี้:

  • Fallback Output: เลือกว่าจะให้ workflow ไปทางไหนถ้า item ไม่ตรงกับ rule หรือเงื่อนไขใดๆ
    • None: ข้าม item นั้น (default)
    • Extra Output: ส่ง item ไป output พิเศษแยกต่างหาก
    • Output 0: ส่ง item ไป output เดียวกับที่ตรงกับ rule แรก
  • Ignore Case: ตั้งค่าว่าจะไม่สนใจตัวพิมพ์เล็ก/ใหญ่ตอนเช็คเงื่อนไข (เปิด = ไม่สนใจ, ปิด = สนใจ)
  • Less Strict Type Validation: ตั้งค่าว่าให้ n8n พยายามแปลง type ของค่าตาม operator ที่เลือก (เปิด = แปลง, ปิด = ไม่แปลง)
  • Send data to all matching outputs: ตั้งค่าว่าจะส่งข้อมูลไปทุก output ที่ตรงเงื่อนไข (เปิด = ส่งทุกอัน, ปิด = ส่งเฉพาะอันแรกที่ตรง)

Expression#

ตั้งค่า node ด้วย operation นี้โดยใช้ parameters เหล่านี้:

  • Number of Outputs: กำหนดจำนวน output ที่ node จะมี
  • Output Index: สร้าง expression เพื่อคำนวณว่า input item ไหนควรไป output ไหน expression ต้องคืนค่าเป็นตัวเลข

Templates and examples#

Building Your First WhatsApp Chatbot

by Jimleuk

View template details
Telegram AI Chatbot

by Eduard

View template details
Respond to WhatsApp Messages with AI Like a Pro!

by Jimleuk

View template details
Browse Switch integration templates, or search all templates

ดู Splitting with conditionals สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้เงื่อนไขสร้าง logic ซับซ้อนใน n8n

การเปรียบเทียบประเภทข้อมูลที่มีอยู่#

String#

ประเภทข้อมูล String รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • contains
  • does not contain
  • starts with
  • does not start with
  • ends with
  • does not end with
  • matches regex
  • does not match regex

Number#

ประเภทข้อมูล Number รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • is greater than
  • is less than
  • is greater than or equal to
  • is less than or equal to

Date & Time#

ประเภทข้อมูล Date & Time รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is equal to
  • is not equal to
  • is after
  • is before
  • is after or equal to
  • is before or equal to

Boolean#

ประเภทข้อมูล Boolean รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty
  • is true
  • is false
  • is equal to
  • is not equal to

Array#

ประเภทข้อมูล Array รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty
  • contains
  • does not contain
  • length equal to
  • length not equal to
  • length greater than
  • length less than
  • length greater than or equal to
  • length less than or equal to

Object#

ประเภทข้อมูล Object รองรับการเปรียบเทียบเหล่านี้:

  • exists
  • does not exist
  • is empty
  • is not empty