Overview

A client script is a piece of JavaScript code that runs on the client, rather than the server. Well-designed client scripts can reduce the amount of time it takes to complete a form. However, improperly implemented client scripts can significantly slow down form load times. With the exception of the CellEdit client script, client scripts apply to forms only.

 Fields in Client Script Form

Type: Indicator of when the script runs.
⦁  onLoad(): Runs when a form is loaded.
⦁  onChange(): Runs when a particular widget changes value.
  onSubmit(): Runs when a form is submitted.
⦁  onCellEdit(): Runs when a cell on a list changes value.
Table: Table or database view this script applies to, such as Incident or Change Request

UI Type: Target UI this script applies to: Desktop, Mobile, or Both

 Client Script Types

onLoad()

An onLoad() script runs when a form is loaded. onLoad() script must contain a function named onLoad(). Otherwise, it is entirely up to you what your script does after it gets to the client.
Example:
function onLoad() {
alert(‘Page is Loading’);
}

onLoad() script that displays a message box that says “Page is Loading ” while the page loads.

onSubmit()

An onSubmit() script Runs when a form is submitted. onSubmit() script must contain a function named onSubmit().
Example:
function onSubmit() {
var urgency = g_form.getValue(‘urgency’);
if (urgency == 1){
return confirm(‘Are you sure you want to submit the form?’);
}
}

onSubmit() the form, If the urgency is 1 then the confirm message popup will display.

 onChange()

An onChange() script can run when a particular field changes value or when the form is first drawn and no fields have changed value from previous script actions. As such, onChange() scripts can potentially act as onLoad() scripts with extra conditions.
An onChange() script must contain a function named onChange().
All onChange() scripts are called with several parameters:

control – the field whose value changed if any.
oldValue – the value of this widget when the record was loaded into the form.
newValue – the value of this widget after the change, or when the isLoading parameter is true, the same value as the oldValue parameter.
isLoading – identifies whether the form is loading and also indicates that no fields have changed value. This parameter can only be true when the form is loading and no values have changed.
isTemplate – identifies whether the change occurs as part of a template load or not.

onCellEdit()

Scripts can be defined as onCellEdit() to run on the client side when the list editor interacts with a cell.
An onCellEdit() script must contain a function named onCellEdit().
An onCellEdit() script takes the following parameters:

sysIDs – An array of the sys_ids for all items being edited.
table – The table of the items being edited.
oldValues – The old values of the cells being edited.
newValue – The new value for the cells being edited.
callback – A callback that continues the execution of any other related cell edit scripts. If ‘true’ is passed as a parameter, the other scripts are executed or the change is committed if there are no more scripts. If ‘false’ is passed as a parameter, any further scripts are not executed and the change is not committed.

Creating a Client Script

⦁ Navigate to System Definition > Client Script.
⦁ Click New.
⦁ Fill in the fields, as appropriate (see table).

⦁ Click Submit.

Sample Client Script

Example: Hide the Closed option in State field when Contact type is email and when contact type is phone hide the resolved option from State field.
Table: Incident
Type: onChange
Field Name: Contact Type
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ’email’) {
g_form.removeOption(‘state’, ‘7’);
return;
}
if (isLoading || newValue === ‘phone’) {
g_form.removeOption(‘state’, ‘6’);
return;
}
}