{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":2451,"path":"application-development/servicenow-sdk/fluent-script-action-api.md","title":"Script Action API - ServiceNow Fluent","bundle":null,"doc_type":null,"release":"australia","body":"\n# Script Action API - ServiceNow Fluent\n\nThe Script Action API defines script actions \\[sysevent\\_script\\_action\\] that run when an event occurs.\n\n**Note:** For the latest ServiceNow Fluent API documentation and examples, see the [ServiceNow Fluent API reference](https://servicenow.github.io/sdk/category/api-reference) and [ServiceNow SDK examples repository](https://github.com/ServiceNow/sdk-examples) on GitHub.\n\nFor general information about scheduled script executions, see [Script actions](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/r_ScriptActions.md).\n\n**Parent Topic:**[ServiceNow Fluent API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/servicenow-fluent-api-reference.md)\n\n**Related topics**  \n\n\n[ServiceNow Fluent](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-fluent.md)\n\n## ScriptAction object\n\nCreate a script action \\[sysevent\\_script\\_action\\] that performs a task when triggered by an event.\n\n<table id=\"table_qyv_1pj_4fc\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n$id\n\n</td><td>\n\nString or Number\n\n</td><td>\n\nRequired. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys\\_id. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).Format: `Now.ID['String' or Number]`\n\n</td></tr><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. A unique name for the script action.\n\n</td></tr><tr><td>\n\nscript\n\n</td><td>\n\nScript\n\n</td><td>\n\nRequired. A server-side script that runs when triggered by an event. This property supports a function from a JavaScript module, a reference to another file in the application that contains a script, or inline JavaScript.Format:\n\n-   For functions, use the name of a function, function expression, or default function exported from a JavaScript module and import it into the `.now.ts` file. For information about JavaScript modules, see [JavaScript modules and third-party libraries](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/javascript-modules-third-party-libraries.md).\n-   To use text content from another file, refer to a file in the application using the following format: `Now.include('path/to/file')`. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).\n-   To provide an inline script, use string literals or template literals for multiple lines of code: `'Script' or `Script``.\n\n</td></tr><tr><td>\n\neventName\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The event that triggers the script action to run. For information about creating events, see [Create an event](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/t_CreateYourOwnEvent.md).\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the script action is enabled.Valid values:\n\n-   true: The script action executes when triggered by the event.\n-   false: The script action doesn't execute.\n\nDefault: false\n\n</td></tr><tr><td>\n\ndescription\n\n</td><td>\n\nString\n\n</td><td>\n\nA description of the functionality and purpose of the script action.\n\n</td></tr><tr><td>\n\norder\n\n</td><td>\n\nNumber\n\n</td><td>\n\nA number indicating the sequence in which the script action should run. If there are multiple script actions on a particular event, the script actions run in the order specified, from lowest to highest.Default: 100\n\n</td></tr><tr><td>\n\nconditionScript\n\n</td><td>\n\nString\n\n</td><td>\n\nA JavaScript conditional statement that specifies the fields and values that must be true for the script to run.**Note:** Don't use this property if you include the condition statement with the script property.\n\nFormat:\n\n-   To use text content from another file, refer to a file in the application using the following format: `Now.include('path/to/file')`. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).\n-   To provide an inline script, use string literals or template literals for multiple lines of code: `'Script' or `Script``.\n\n</td></tr><tr><td>\n\n$meta\n\n</td><td>\n\nObject\n\n</td><td>\n\nMetadata for the application metadata.With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances.\n\n```javascript\n$meta: {\n      installMethod: 'String'\n}\n```\n\nValid values for installMethod:\n\n-   demo: Outputs the application metadata to the `metadata/unload.demo` directory to be installed with the application when the **Load demo data** option is selected.\n-   first install: Outputs the application metadata to the `metadata/unload` directory to be installed only the first time an application is installed on an instance.\n\n</td></tr></tbody>\n</table>```javascript\nimport { ScriptAction } from '@servicenow/sdk/core'\nimport { insertIncident } from '../server/scripts.js'\n\nScriptAction({\n    $id: Now.ID['sample-script-action'],\n    name: 'SampleScriptAction',\n    active: true,\n    description: 'Insert an incident',\n    script: insertIncident,\n    eventName: 'sample.event',\n    order: 100,\n    conditionScript: \"gs.hasRole('my_role')\"\n})\n```\n\nThe script property refers to a function from the `scripts.js` module. For example:\n\n```javascript\nimport { GlideRecord } from '@servicenow/glide'\n\nexport const insertIncident = () => {\n    var gr = new GlideRecord('incident')\n    gr.initialize()\n    gr.setValue('short_description', 'New incident from event')\n    gr.insert()\n}\n```\n\n"}}