Execute triggers conditionally

  • 릴리스 버전: Australia
  • 업데이트 날짜 2026년 03월 12일
  • 소요 시간: 5분
  • MetricBase triggers execute based on a single metric. Condition Scripts impose additional requirements that determine whether a trigger kicks off a flow.

    시작하기 전에

    Role required: admin

    이 태스크 정보

    Condition Scripts execute when conditions for a trigger are met but before the trigger executes a Workflow Studio flow. In that way, Condition Scripts can prevent triggers from executing flows even when trigger conditions are met. For example, data often fluctuates over time. Small fluctuations can cause unwanted, duplicate triggering events. A Condition Script can prevent that erroneous duplication.

    Condition scripts are sometimes also referred to as moderator scripts.

    Condition Scripts always return true (trigger) or false (do not trigger). To learn how to write these scripts, see Scripting in ServiceNow Fundamentals. To experiment with scripts, see Get familiar with MetricBase APIs.

    프로시저

    1. Navigate to All > MetricBase > MetricBase Triggers > Trigger Condition Script.
    2. Select New.
    3. On the form, fill in the fields.
      Field Description
      Name Name for the Condition Script.
      Application Scope of the Condition Script. The value, Global, means that the action applies to all applications.
      Description Explanation of what the Condition Script does. When does it return True or False?
      Script Field to enter the JavaScript. Make it returns true to execute a flow.
    4. Write the Condition Script.

      When writing a script, think about the conditional statements (what cases it should execute for and what cases it shouldn’t). If all evaluate true, the script returns true and the flow executes, otherwise it does not. The following example script triggers a flow when a drone is traveling too fast at a low altitude (defined by level 1). The example shows a typical approach to writing a condition script.

      1. Get the trigger definition, passed in as a filter function parameter.
      2. Get the record (current) that is causing the triggering event, passed in as a filter function parameter.
      3. Get the time from the record that the trigger conditions were satisfied, passed in as a filter function parameter.
      4. Get the trigger level, passed in as a filter function parameter.
      5. Use these parameters to return true if the level 1 trigger conditions are met and travel_state equals traveling or speeding.
        function filter(/*GlideRecord*/ triggerDefinition, /*GlideRecord*/ current, /*GlideDateTime*/ start, /*int*/ level) {
        	// retrieve current travel state of drone
        	var travel_state = String(current.travel_state);
        	
        	// the drone is traveling at a significant speed, and the altitude just went below the threshold 
        	if (((travel_state === 'traveling') || (travel_state === 'speeding')) && (level === 1)){
        		return true; //process this trigger
        	}
        	
        	return false; // don't process this trigger
        }
        
      주:
      Condition Scripts must execute quickly.
    5. Select Submit.

    다음에 수행할 작업

    Use Workflow Studio to associate a flow with a trigger. When configuring a flow, you can select a Condition Script you created.
    그림 1. Add a Condition Script to a trigger definition in Workflow Studio
    Add Condition Script to a trigger definition.

    You can also associate a condition script with a trigger flow in the MetricBase Trigger Flows [sys_flow_metric_trigger] table. If you associate a condition script with a trigger flow here, it won’t appear in Workflow Studio, but it will still execute with the trigger.

    그림 2. Associate a Condition Script in MetricBase Trigger Flows
    Associate a Condition Script in MetricBase trigger flows table