Servicenow Client Script

Servicenow Client Script

In this Blog , we will start taking a look at Servicenow Client Script basics. This will give you an idea of where and how to write your client side scripts and how to test them. We’ll also take a look at a few examples for getting started in this area.

Servicenow Client Script

Client scripts are generally the client-side scripts most used by developers. By default, a client script will run on all views of a form, but it can be set up to only run for a certain view by unchecking the global checkbox and entering a specific view into the view field that appears. Client scripts can also be inherited to tables that extend from a different table. For example, a client script on the task table can be inherited to tables that extend it, like the incident, change, and problem tables. To allow tables to inherit a client script, the inherit checkbox must be set. The script in a client script can be run at different times, depending on when the developer chooses. This choice is made by selecting a type for the client script. There are four types of client script, and these are:

  1. onLoad
  2. onChange
  3. onSubmit
  4. onCellEdit


The onLoad client script type runs script when a form is loaded. This can be helpful to manipulate data on a form before a user starts using it. Sometimes, when a form loads slowly, it is possible to see onLoad client scripts doing their job.

When selecting the onLoad type, the script field will be populated with the onLoad function ServiceNow provides for you, as long as the script field hasn’t already been edited. The script looks as follows:

function onLoad() {
//Type appropriate comment here, and begin script below


This is the onLoad function to put your script into. Any script running onLoad will take time to run and slow down loading time, so it is always worth trying to keep onLoad client scripts to a minimum, and short, if necessary. I have been involved in projects simply to reduce the amount of onLoad client scripts on forms to reduce load times.

Sample onLoad client script
Sample onLoad client script

In the preceding figure, we can see an onLoad client script ready to be written for the incident table. As the comment states in the code, replace the comment in the code with the functionality of your script, and then begin writing underneath the comment inside the onLoad function.


The onChange client script runs when a selected field is changed on the form. It is important to note that an onChange script will also run when the form is loaded. This type of script is often used to auto-populate other fields on a form, based on data in the field the onChange script is running on. For example, if a user is selected on a form, other user data on the form, like company and job title, could be populated using an onChange script.

In the same way that ServiceNow provides a script function in onLoad scripts, an onChange function is also provided. This is slightly more complicated, so let’s take a look:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {

//Type appropriate comment here, and begin script below


The onChange function gives us five parameters that we can use inside the function in our script. Here is what each of these parameters gives us:

Control: The dynamic HTML of the field that has changed
oldValue: The value in the changed field when the form was loaded
newValue: The new value that has been entered into the changed field
isLoading: Is true if the form is loading; otherwise, false
isTemplate: Is true if the change has occurred as part of a template load; otherwise, false

You can also see that a little scripting has already been done by ServiceNow. This if statement checks if the form is loading or if the new value of the changed field is empty. If either of these cases is true, the script returns from the function, essentially canceling the script. In most cases, this is quite helpful, but it is good to understand this if statement, as sometimes you will want to amend it if you want code to run during loading or on the field changing to an empty value.

For example, if you have cleared a user field where additional user data has been added to the form, you will also want to clear the additional user data. In this instance, you will want to remove the condition where the code returns from the function if the newValue parameter is blank.

When selecting the onChange type, the option to pick a field for the script to run against will be visible to the developer. The fields available in this field are dependent on the value in the table field.

Sample onChange client script
Sample onChange client script

In the preceding figure, we can see an onChange client script for the problem table. Pay particular attention to the Field name field, which defaults to Active, as that is the field that needs to change for this script to run. It can be easy to forget to change this field and wonder why your script is not executing when you are expecting it to.


The onSubmit client script type runs when a form is saved. This type of script is not that widely used, as we can often use server-side script to perform actions for us once a record is saved, but it can be helpful, as it provides a final chance to execute client-side script before server-side script is run.

The script ServiceNow provides for onSubmit is quite similar to onLoad:

function onSubmit() {
//Type appropriate comment here, and begin script below


The onSubmit script type can be used to check field values before a save takes place and abort the save if invalid values exist in fields.

When considering writing an onSubmit script, it is worth thinking about whether that script could run on the server side instead. This is because the client script runs in front of the user, and further processing must wait until the script finishes, whereas on the server side, the code is executed away from the user and can be executed alongside other scripts.

Sample onSubmit client script
Sample onSubmit client script


For our onSubmit client script example, we can see that this client script is ready for the script to be added for the change table.


The onCellEdit type of client script runs when a cell is edited on the list view of a table. This is the only client script type that does not run on the form view of a table. This type of client script is helpful to ensure that the rules you want to stick to are also enforced on list views of a table.

The script that ServiceNow provides for onCellEdit scripts looks a little different from the other client script types:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
//Type appropriate comment here, and begin script below


It is worth noting that these scripts could be for multiple records, as you can edit more than one using a list. As you can see, there are quite a few different parameters in this slightly different type of script. We’ll have a look at what each one of those parameters gives us:

sysIDs: All of the sys_id unique IDs of the records being edited in an array.
table: The current table of the records being edited.
oldValues: The old values of all the cells being edited.
newValue: The new value to be put into all the cells being edited.
callback: A callback that allows the execution of further onCellEdit scripts or commits the change made if no further scripts exist. A true or false parameter can be passed, which will either allow further scripts and commit changes, or stop execution of further scripts and not commit the change, respectively.

These scripts tend to not be used that frequently, as server-side scripts like business rules can often be used to perform the functionality needed. Editing records in the list can also be a powerful tool for updating multiple records at once, so this is sometimes locked down to only certain users to prevent less knowledgeable users from causing issues.

Sample onCellEdit client script
Sample onCellEdit client script

The client script in the preceding figure is an onCellEdit for the requested item table.  Again, for this type of script, ensure the Field name field is set to the value you require, as the script will only run on a change to this field in the list view.


Please enter your comment!
Please enter your name here