{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":48004,"path":"servicenow-platform/metricbase/create-action-condition.md","title":"Execute triggers conditionally","bundle":null,"doc_type":null,"release":"australia","body":"\n# Execute triggers conditionally\n\nMetricBase triggers execute based on a single metric. Condition Scripts impose additional requirements that determine whether a trigger kicks off a flow.\n\n## Before you begin\n\nRole required: admin\n\n## About this task\n\nCondition 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.\n\nCondition scripts are sometimes also referred to as moderator scripts.\n\nCondition Scripts always return *true* \\(trigger\\) or *false* \\(do not trigger\\). To learn how to write these scripts, see [Scripting in ServiceNow Fundamentals](https://www.servicenow.com/services/training-and-certification/scripting-in-servicenow-training.html). To experiment with scripts, see [Get familiar with MetricBase APIs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/metricbase/metricbase-data-explorer.md).\n\n## Procedure\n\n1.  Navigate to **All** &gt; **MetricBase** &gt; **MetricBase Triggers** &gt; **Trigger Condition Script**.\n\n2.  Select **New**.\n\n3.  On the form, fill in the fields.\n\n    |Field|Description|\n    |-----|-----------|\n    |Name|Name for the Condition Script.|\n    |Application|Scope of the Condition Script. The value, **Global**, means that the action applies to all applications.|\n    |Description|Explanation of what the Condition Script does. When does it return True or False?|\n    |Script|Field to enter the JavaScript. Make it returns *true* to execute a flow.|\n\n4.  Write the Condition Script.\n\n    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.\n\n    1.  Get the trigger definition, passed in as a filter function parameter.\n\n    2.  Get the record \\(*current*\\) that is causing the triggering event, passed in as a filter function parameter.\n\n    3.  Get the time from the record that the trigger conditions were satisfied, passed in as a filter function parameter.\n\n    4.  Get the trigger level, passed in as a filter function parameter.\n\n    5.  Use these parameters to return *true* if the level 1 trigger conditions are met and *travel\\_state* equals traveling or speeding.\n\n        ```\n        function filter(/*GlideRecord*/ triggerDefinition, /*GlideRecord*/ current, /*GlideDateTime*/ start, /*int*/ level) {\n        \t// retrieve current travel state of drone\n        \tvar travel_state = String(current.travel_state);\n        \t\n        \t// the drone is traveling at a significant speed, and the altitude just went below the threshold \n        \tif (((travel_state === 'traveling') || (travel_state === 'speeding')) && (level === 1)){\n        \t\treturn true; //process this trigger\n        \t}\n        \t\n        \treturn false; // don't process this trigger\n        }\n        \n        ```\n\n    **Note:** Condition Scripts must execute quickly.\n\n5.  Select **Submit**.\n\n\n## What to do next\n\nUse Workflow Studio to [associate a flow with a trigger](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/metricbase/assign-trigger-to-workflow.md). When configuring a flow, you can select a Condition Script you created.\n\n\\[Omitted image \"condition-script-flow-designer.png\"\\] Alt text: Add Condition Script to a trigger definition.\n\nYou 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.\n\n\\[Omitted image \"condition-script-trigger-flow.png\"\\] Alt text: Associate a Condition Script in MetricBase trigger flows table\n\n"}}