Configuring conditional script in service now trigger

SiluniP
Tera Contributor

I need to trigger an email notification based on a few conditions being met.

I have used the ServiceNow Workflow Studio to implement the trigger, and now I need to add some advanced conditions so that the trigger runs only when those conditions are satisfied.

I couldn't find any place to attach a conditional script. I would appreciate any input related to configuring a script for a ServiceNow trigger.

3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @SiluniP,

 

In ServiceNow, conditional scripts in triggers, often referred to as "Condition Scripts," determine whether a flow should execute based on specific conditionsThese scripts are written in JavaScript and evaluate to true or false, controlling whether the main flow action is executed. 
 
Here's how to configure a conditional script in a ServiceNow trigger:
  1. Access Flow Designer: Open the Flow Designer in ServiceNow. 
     
  2. Add a Trigger: Click "Add a trigger" and choose the desired trigger type (e.g., record, event, etc.). 
     
  3. Configure the Trigger: Expand the trigger's configuration options by clicking on it. 
     
  4. Add Conditional Script: Locate the field for "Condition Script" or "Conditions." This field is where you'll write your JavaScript code. 
     
  5. Write the Script: Write the JavaScript code to evaluate the conditions. The script should return true for the flow to execute and false to prevent execution. 
     
  6. Example:
     
 
   // This example checks if the current date is a weekday.   var now = new GlideDateTime();   var dayOfWeek = now.getDay(); // 0 (Sunday) - 6 (Saturday)   if (dayOfWeek >= 1 && dayOfWeek <= 5) {       answer = true; // Set to true to execute the flow   } else {       answer = false; // Set to false to prevent execution   }
  1. Save the Flow: Save your flow to activate the conditional script. 
     
Key points about conditional scripts:
  • Scope:
    Variables declared in the Condition Script are typically known within the Run this script section, if permitted by JavaScript scope rules. 
     
  • Default Behaviour:
    If no condition script is provided, the script in the "Run this script" field will execute by default. 
     
  • Purpose:
    Condition scripts help you separate conditional logic, preventing excessive nesting and improving code readability. 
     
  • Error Handling:
    If the Condition Script encounters an error, it may prevent the flow from executing or might produce unexpected results. 
     
    If it is helpful, please hit the thumbs up icon and accept the correct answer by referring to this solution in future, it will be helpful to them.
     
    Thanks & Regards,
    Abbas Shaik

Swapna Abburi
Mega Sage
Mega Sage

Hi @SiluniP 

I think it is not possible to use script based condition in Trigger type of flow designers. You can consider using a scheduled job.

Ankur Bawiskar
Tera Patron
Tera Patron

@SiluniP 

Please share some screenshots etc what you tried and what's not working

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader