{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":2436,"path":"application-development/servicenow-sdk/client-script-api-now-ts.md","title":"Client Script API - ServiceNow Fluent","bundle":null,"doc_type":null,"release":"australia","body":"\n# Client Script API - ServiceNow Fluent\n\nThe Client Script API defines client-side scripts \\[sys\\_script\\_client\\] that run JavaScript on the client \\(web browser\\) when client-based events occur, such as when a form loads, after form submission, or when a field changes value.\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 client scripts, see [Client scripts](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/scripts/client-scripts.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## ClientScript object\n\nCreate a client script \\[sys\\_script\\_client\\] to configure forms, form fields, and field values while the user is using the form.\n\n<table id=\"table_vy2_525_jq\" 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\ntable\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The name of the table on which the client script runs.\n\n</td></tr><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The name of the client script.\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the client script is enabled.Valid values:\n\n-   true: The script is enabled.\n-   false: The script isn't enabled.\n\nDefault: true\n\n</td></tr><tr><td>\n\nappliesExtended\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the client script applies to tables extended from the specified table.Valid values:\n\n-   true: The script applies to extended tables.\n-   false: The script doesn't apply to extended tables.\n\nDefault: false\n\n</td></tr><tr><td>\n\nuiType\n\n</td><td>\n\nString\n\n</td><td>\n\nThe user interface to which the client script applies.Valid values:\n\n-   desktop\n-   mobile\\_or\\_service\\_portal\n-   all\n\nDefault: desktop\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 client script.\n\n</td></tr><tr><td>\n\nmessages\n\n</td><td>\n\nString\n\n</td><td>\n\nText strings that are available to the client script as localized messages using getmessage\\('\\[message\\]'\\). For more information, see [Translate a client script message](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/t_TranslateAClientScriptMessage.md).\n\n</td></tr><tr><td>\n\nisolateScript\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the script runs in strict mode, with access to direct DOM, jQuery, prototype, and the window object turned off.Valid values:\n\n-   true: Isolate the script and don't run it in strict mode.\n-   false: Run the script in strict mode.\n\nDefault: false\n\n</td></tr><tr><td>\n\nscript\n\n</td><td>\n\nScript\n\n</td><td>\n\nA client-side script that runs in the browser. This property supports inline JavaScript or a reference to another file in the application that contains a script.Format:\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\nglobal\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates on which views of the table the client script runs.Valid values:\n\n-   true: The script runs on all views.\n-   false: The script runs only on the specified views.\n\nDefault: true\n\n</td></tr><tr><td>\n\nview\n\n</td><td>\n\nString\n\n</td><td>\n\nThe views of the table on which the client script runs. This property applies only when the global property is set to false.\n\n</td></tr><tr><td>\n\ntype\n\n</td><td>\n\nString\n\n</td><td>\n\nThe type of client script, which defines when it runs. For more information about the supported types, see [Client scripts](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/scripts/client-scripts.md).Valid values:\n\n-   onCellEdit: Runs when the list editor changes a cell value.\n-   onChange: Runs when a particular field value changes on the form.\n-   onLoad: Runs when the system first renders the form and before users can enter data. Typically, onLoad\\(\\) client scripts perform client-side-manipulation of the current form or set default record values.\n-   onSubmit: Runs when a form is submitted. Typically, onSubmit\\(\\) scripts validate things on the form and ensure that the submission makes sense. An onSubmit\\(\\) client script can cancel form submission by returning a value of false.\n\n</td></tr><tr><td>\n\nfield\n\n</td><td>\n\nString\n\n</td><td>\n\nA field on the table that the client script applies to. This property applies only when the type property is set to onChange or onCellEdit.\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 { ClientScript } from '@servicenow/sdk/core'\n\nexport const cs = ClientScript({\n   $id: Now.ID['my_scripts'], \n   name: 'my_scripts',\n   table: 'incident',\n   active: true, \n   appliesExtended: false, \n   global: true,\n   uiType: 'all', \n   messages: '', \n   isolateScript: false, \n   type: 'onLoad',\n   script: Now.include\\('../client/client-script.js'\\),\n})\n```\n\nThe client script is defined in the `client-script.js` file referenced from the script property. For example:\n\n```javascript\nfunction onLoad() { \n    const x = 'util' g_form.addInfoMessage(x) \n}\n```\n\n"}}