CLIENT SCRIPT
A client-side script that runs JavaScript on the client (web browser) when client-based events
occur.
Client scripts are used to customize the behavior of a form or other UI element.
They can be triggered by a user action such as clicking a button or by an event such as the loading of a
page.
Some use cases for client scripts include:
Placing the cursor in a form field on form load
Generating alerts, confirmations, and messages
Populating a form field in response to another field's value
Highlighting a form field
Validating form data
Modifying choice list options
Making a field read only, hidden, or mandatory
Displaying informational messages
Generating pop ups on button click or on page load
Note *** Client scripts are executed by the browser. It is important to make sure any DOM calls are as
technology agnostic as possible.
Where client scripts run
With the exception of onCellEdit() client scripts, client scripts only apply to forms and search pages.
If you create a client script to control field values on a form, you must use one of these other methods to control field values
when on a list.
Create an access control to restrict who can edit field values.
Create a business rule to validate content.
Create a data policy to validate content.
Create an onCellEdit() client script to validate content.
Disable list editing for the table.
Note: Client scripts are not supported on ServiceNow mobile applications.
UI Type : Select whether the script Inherited: If selected, this script applies to the specified table and all
executes for Desktop and tables that inherit from it. For example, a client script on the Task table
Tablet or Mobile/Service Portal or All. will also apply to the Change, Incident, Problem and all other tables
which extend Task.
Type: Select when the script Global : Script runs on all views of table.
runs: onChange, onLoad, or onSubmit.
View: Specifies the View to which the script applies
Client Script Type
Type Description Diagram
onLoad Execute when form loaded.
Use onLoad Client Scripts to manipulate a form's
appearance or content. For example, setting field or form-
level messages based on the presence of a value.
Use onLoad Client Scripts sparingly as they impact form
load times.
onChange Execute when particular field’s value change.
Use onChange Client Scripts to respond to field values of
interest and to modify another field's value or attributes
For example, if the State field's value changes to Closed
Complete, generate an alert and make the Description field
mandatory.
onSubmit Execute script logic when a form is submitted.
Use onSubmit Client Scripts to validate field values. For
example, if a user submits a Priority 1 record, the script
can generate a confirmation dialog notifying the user that
the executive staff are copied on all Priority 1 requests.
THE SCRIPT FIELD
onLoad Script Template
onSubmit Script Template
onChange Script Template
control : the DHTML (Dynamic Hyper Text Markup Language) widget whose value changed.
oldValue : value of the field when the form loaded and prior to the change.
newValue : value of the field after the change.
isLoading : boolean value indicating whether the change is occurring as part of a form load. Value is true if
change is due to a form load. When forms load, all the field values on the form change as the record is
loaded into the form.
isTemplate : boolean value indicating whether the change occurred due to population of the field by a
template. Value is true if change is due to population from a template.
onCellEdit Script Template
sysIDs : an array of the sys_ids for all items parameters
table : the table of the items being edited.
oldValues : the old value for the cells being edited.
newValues : 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.
Note : onCellEdit() client script cann’t apply to dashboard list widgets.
Example of onCellEdit() Client Script :
Calling in Client Script
Server Side Script Client Side Script
Business Rule Using : g_scratchpad
(function executeRule(current, previous /*null when async*/ ) { function onLoad() {
var gr = new GlideRecord("problem"); //Type appropriate comment here, and begin script below
if (gr.get(current.parent)) { if (g_scratchpad.parent) {
g_scratchpad.parent = current.parent; g_form.setValue("short_description", g_scratchpad.short_description);
g_scratchpad.short_description = gr.short_description; g_form.setValue("assignment_group", g_scratchpad.assignment_group);
g_scratchpad.assigned_to = gr.assigned_to; g_form.setValue("assigned_to", g_scratchpad.assigned_to);
g_scratchpad.assignment_group = gr.assignment_group; }
} }
})(current, previous);
Script Include Using : GlideAjax (*** this is catalog client script)
var listCollectorUseCase = Class.create(); function onChange(control, oldValue, newValue, isLoading) {
listCollectorUseCase.prototype = Object.extendsObject(AbstractAjaxProcessor, if (isLoading || newValue == '') {
{ return;
}
listCollectorUser: function() { g_form.addInfoMessage(g_form.getValue("trainees_users"));
var traineesUser = this.getParameter('Trainees'); //Parameter passed at //Type appropriate comment here, and begin script below
Client Side 'Trainees' var rec = new GlideAjax('listCollectorUseCase');
var traineeUsr = traineesUser.split(','); rec.addParam('sysparm_name', 'listCollectorUser');
var traineesRecord = []; rec.addParam('Trainees', g_form.getValue("trainees_users"));
rec.getXMLAnswer(response);
for (var i = 0; i < traineeUsr.length; i++) {
var userRec = new GlideRecord('sys_user'); function response(result) {
userRec.addActiveQuery(); alert('Check Result ' + result);
userRec.addQuery('sys_id', traineeUsr[i]) g_form.setValue('trainees_email_id', result);
userRec.query(); }
while (userRec.next()) { }
traineesRecord.push(userRec.email);
}
}
return traineesRecord.toString();
},
type: 'listCollectorUseCase '
});
onSubmit Client Script onLoad Client Script
function onSubmit() { function updateGrandTotal(totalCost) {
var totalCost = 10; g_form.setValue('grand_total', totalCost);
updateGrandTotal(totalCost); }
}
function onLoad() {