Skip to main content
Version: Latest (4.5.0)

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): string Name of the client script

  • table (required): keyof Tables Name 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'
  • active (optional): boolean Whether the record is enabled

  • appliesExtended (optional): boolean Indicates whether the client script applies to tables extended from the specified table

  • description (optional): string Description 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 to onChange or onCellEdit.

  • global (optional): G Indicates which views of the table the client script runs.

    • true: the script runs on all views
    • false: the script runs only on specified views
  • isolateScript (optional): boolean Indicates whether scripts run in strict mode, with access to direct DOM, jQuery, prototype, and the window object turned off

  • messages (optional): string Strings that are available to the client script as localized messages using getmessage('[message]').

  • script (optional): string Script to be executed when the client script runs

  • type (optional): client_script_events Type 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 : never Views of the table on which the client script runs. This property applies only when the global property is set to false

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')
}