Function: ClientScript(properties)
Creates a Client Script (sys_script_client).
Parameters
properties
ClientScriptProps<keyof Tables, client_script_events, G>
Properties:
-
$id (required):
string | number | ExplicitKey<string> -
name (required):
stringName of the client script -
table (required):
keyof TablesName of the table on which the client script runs -
$meta (optional):
object- installMethod:
'first install' | 'demo'Map a record to an output folder that loads only in specific circumstances. 'first install' - > 'unload', 'demo' -> 'unload.demo'
- installMethod:
-
active (optional):
booleanWhether the record is enabled -
appliesExtended (optional):
booleanIndicates whether the client script applies to tables extended from the specified table -
description (optional):
stringDescription of the functionality and purpose of the client script -
field (optional):
field_type<keyof Tables, client_script_events>Field on the table that the client script applies to. This property applies only when the type property is set toonChangeoronCellEdit. -
global (optional):
GIndicates which views of the table the client script runs.true: the script runs on all viewsfalse: the script runs only on specified views
-
isolateScript (optional):
booleanIndicates whether scripts run in strict mode, with access to direct DOM,jQuery,prototype, and thewindowobject turned off -
messages (optional):
stringStrings that are available to the client script as localized messages usinggetmessage('[message]'). -
script (optional):
stringScript to be executed when the client script runs -
type (optional):
client_script_eventsType of client script, which defines when it runs -
uiType (optional):
'desktop' | 'all' | 'mobile_or_service_portal'User interface to which the client script applies -
view (optional):
G extends false ? string : neverViews of the table on which the client script runs. This property applies only when theglobalproperty is set tofalse
See
Examples
OnLoad Client Script with Inline Script
Create an onLoad client script with inline JavaScript code
/**
* @title OnLoad Client Script with Inline Script
* @description Create an onLoad client script with inline JavaScript code
*/
import { ClientScript } from '@servicenow/sdk/core'
ClientScript({
$id: Now.ID['cs0'],
name: 'my_client_script',
table: 'incident',
active: true,
applies_extended: false,
global: true,
ui_type: 'all',
description: 'Custom client script generated by Now SDK',
messages: '',
isolate_script: false,
type: 'onLoad',
script: script`function onLoad() {
g_form.addInfoMessage("Table loaded successfully!!")
}`,
})
OnChange Client Script
Create a client script that triggers when a field value changes
/**
* @title OnChange Client Script
* @description Create a client script that triggers when a field value changes
*/
import { ClientScript } from '@servicenow/sdk/core'
ClientScript({
$id: Now.ID['onchange_script'],
name: 'field_change_handler',
table: 'incident',
active: true,
type: 'onChange',
field: 'priority',
ui_type: 'desktop',
description: 'Handles priority field changes',
script: script`function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
if (newValue === '1') {
g_form.addWarningMessage('High priority incident - please add urgency details');
}
}`,
})
OnLoad Client Script with External File
Create an onLoad client script that uses an external JavaScript file
/**
* @title OnLoad Client Script with External File
* @description Create an onLoad client script that uses an external JavaScript file
*/
import { ClientScript } from '@servicenow/sdk/core'
export default ClientScript({
$id: Now.ID['sample1'],
type: 'onLoad',
ui_type: 'all',
table: 'incident',
global: true,
name: 'sample_client_script',
active: true,
applies_extended: false,
description: 'sample client script',
isolate_script: false,
script: Now.include('../../server/ClientScript/clientscript-onload.client.js'),
})
clientscript-onload.client.js
function onLoad() {
g_form.addInfoMessage('Hello from Fluent Client Script')
}