استخدام IF وSwitch وبناء المسارات الشرطية في n8n | Using IF and Switch for Conditional Workflows in n8n
تعلّم ببساطة، خطوة بخطوة
Learn simply, step by step
معلومة واضحة، ثم خطوة جديدة.
Clear knowledge, one step at a time.
استخدام IF وSwitch وبناء المسارات الشرطية في n8n Using IF and Switch for Conditional Workflows in n8n
ما هي المسارات الشرطية في n8n؟
تساعدك المسارات الشرطية في n8n على جعل الـWorkflow يتخذ قرارًا تلقائيًا بناءً على البيانات التي يستقبلها. فبدلًا من تنفيذ نفس الخطوات في جميع الحالات، يمكن توجيه البيانات إلى مسارات مختلفة حسب شروط تحددها أنت.
ومن أهم الأدوات المستخدمة لهذا الغرض عقدتا IF وSwitch.
استخدام عقدة IF في n8n
تُستخدم عقدة IF عندما تريد اختبار شرط وتحديد ما إذا كانت النتيجة صحيحة أو خاطئة. بعد فحص البيانات، يستطيع الـWorkflow متابعة التنفيذ في المسار المناسب.
مثال بسيط: لديك Workflow يستقبل سعر عملة رقمية. يمكنك إنشاء شرط للتحقق مما إذا كان السعر أكبر من قيمة معينة.
- إذا تحقق الشرط، ينتقل التنفيذ إلى المسار الأول.
- إذا لم يتحقق، ينتقل إلى المسار الآخر.
متى نستخدم Switch؟
عندما يكون لديك أكثر من احتمالين، تصبح عقدة Switch أكثر ملاءمة. فهي تسمح بتوجيه البيانات إلى عدة مسارات بدلًا من الاعتماد على نتيجة صحيحة أو خاطئة فقط.
على سبيل المثال، يمكن أن يستقبل Workflow نوع طلب من المستخدم مثل:
- Crypto
- AI
- Security
ثم تستخدم Switch لتوجيه كل نوع إلى مجموعة مختلفة من الخطوات داخل نفس الـWorkflow.
IF أم Switch؟
استخدم IF عندما يكون القرار بسيطًا وله مساران أساسيان، مثل تحقق الشرط أو عدم تحققه. أما إذا كانت البيانات يمكن أن تؤدي إلى عدة نتائج مختلفة، فعادةً تكون Switch أكثر تنظيمًا.
بناء المسارات الشرطية في n8n بطريقة صحيحة
ابدأ بشرط بسيط واختبر البيانات التي تصل إلى العقدة قبل إضافة المزيد من الفروع. تأكد أيضًا من نوع القيمة التي تقارنها، مثل النص أو الرقم، لأن استخدام نوع بيانات غير مناسب قد يؤدي إلى نتيجة مختلفة عن المتوقع.
بعد نجاح الشرط الأول، يمكنك إضافة إجراءات مختلفة لكل مسار وربطها بعقد أخرى أو APIs حسب وظيفة الـWorkflow.
مثال عملي
تخيل Workflow يستقبل قيمة صفقة. إذا كانت القيمة أكبر من حد معين، يرسل إشعارًا للمراجعة. وإذا كانت أقل من الحد، يكمل العملية العادية تلقائيًا.
بهذه الطريقة لا يقوم الـWorkflow بتنفيذ الأوامر فقط، بل يستطيع اتخاذ قرارات مختلفة بناءً على البيانات.
لمزيد من التفاصيل حول توجيه البيانات والشروط، يمكنك مراجعة
توثيق n8n الرسمي الخاص بتقسيم وتوجيه البيانات.
ويمكنك أيضًا تجربة
أدوات TheCrypTechAI المجانية
للتعرف على أمثلة مختلفة للأدوات والعمليات التي يمكن تطوير أتمتة لها.
الخلاصة
تجعل المسارات الشرطية في n8n الـWorkflow أكثر ذكاءً ومرونة. استخدم IF للقرارات البسيطة ذات المسارين، واستخدم Switch عندما تحتاج إلى توجيه البيانات بين عدة مسارات مختلفة.
What Are Conditional Workflows in n8n?
Conditional Workflows in n8n allow your workflow to make automatic decisions based on the data it receives. Instead of running the same steps every time, you can route data through different paths depending on conditions you define.
Two of the most useful nodes for this purpose are IF and Switch.
Using the IF Node in n8n
The IF node is useful when you want to test a condition and determine whether the result is true or false. After checking the data, the workflow can continue through the appropriate path.
For example, imagine a workflow that receives the price of a cryptocurrency. You can create a condition to check whether the price is greater than a specific value.
- If the condition is true, the workflow follows the first path.
- If the condition is false, it follows the other path.
When Should You Use Switch?
When you have more than two possible outcomes, the Switch node can be more suitable. It allows you to route data through multiple paths instead of relying only on a true or false result.
For example, a workflow could receive a request type such as:
- Crypto
- AI
- Security
You can then use Switch to send each request type to a different set of steps within the same workflow.
IF or Switch?
Use IF when you have a simple decision with two main outcomes, such as whether a condition is met or not. If your data can lead to several different outcomes, Switch usually provides a cleaner way to organize the workflow.
Building Conditional Workflows in n8n Correctly
Start with a simple condition and inspect the data entering the node before adding more branches. Also check the type of value you are comparing, such as text or numbers, because using the wrong data type can produce unexpected results.
Once the first condition works correctly, you can add different actions to each path and connect them to other nodes or APIs depending on the purpose of your workflow.
Practical Example
Imagine a workflow that receives a transaction value. If the value is above a certain limit, the workflow sends a notification for review. If it is below the limit, the normal process continues automatically.
In this way, the workflow does more than execute commands. It can make different decisions based on incoming data.
For more information about routing and splitting data, visit the
official n8n documentation about splitting and routing data.
You can also explore
TheCrypTechAI free online tools
to see different examples of tools and processes that could be automated.
Summary
Conditional Workflows in n8n make your workflows more flexible and capable of making decisions. Use IF for simple two-path decisions and Switch when you need to route data through several different paths.
اختبر فهمك
Check Your Understanding
سؤالان سريعان لتثبيت أهم ما تعلمته.
Two quick questions to reinforce the key ideas.