The web blog http://www.learnsccd.com strives to be a comprehensive guide to SCCD, TADDM, SERVICE NOW and MAXIMO tools. This is a personal site maintained solely by me. I intend to update it regularly.For live updates please follow us on.

Service Now - Client Script

Service Now - Client Script

Client scripts are shipped to the client (the browser) and run there instead of on the server. Users who know JavaScript can define scripts to run in the client browser. Several types of scripts are supported.

  • 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.

Client Script Types

1) onLoad() Scripts
An onLoad() script runs when a form is first drawn and before control is given to the user to begin typing. Typically, you use an onLoad() script to perform some client side manipulation of the document on screen.

An 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.

For example, here is a trivial onLoad() script that displays a message box that says "Loading ..." while the page loads.

function onLoad() {
 alert('Loading ...');
}

2) onSubmit() Scripts
An onSubmit() script runs when a form is submitted. Typically, you use an onSubmit() script to validate things on the form and ensure that the submission makes sense.
As such, onSubmit() scripts can potentially cancel a submission by returning false.
An onSubmit() script must contain a function named onSubmit().

For example, here is an onSubmit() script that prompts the user to confirm that a priority one ticket should really be submitted. If the user clicks Cancel in the confirmation dialog box, the submission is canceled.

function onSubmit() {
 var priority = g_form.getValue('priority');
 if (priority && priority == 1) return confirm('Are you sure you want to submit a priority one ticket? The CIO will be notified!');}
 }

3) onChange() Scripts
Unlike onLoad() and onSubmit() scripts, onChange() scripts apply to a particular widget on a form rather than to the form itself. They are fired when a particular value changes on-screen.
An onChange() script must contain a function named onChange().

For example, here is an onChange() script that notifies the user whenever the short description field on a form changes.

function onChange(control, oldValue, newValue, isLoading) {
 alert('you changed short description from ' + oldValue + ' to ' + newValue);
 }
To prevent an onChange() script from running when the form loads, add the following to the top of the script.
if (isLoading) {
return;
 }

4) onCellEdit() Scripts
Scripts can be defined as onCellEdit to run on the client side when the list editor interacts with a cell. This option is available starting with the Aspen release.
An onCellEdit() script must contain a function named onCellEdit().

Example:
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var hasDifferentValues = false;
for (var i = 0; i < oldValues.length; i++) {
 var oldValue = oldValues[i];
  if (oldValue != newValue){
  hasDifferentValues = true;
  break;
  }
 }
var success = hasDifferentValues && performSomeFurtherValidation(sysIDs, table, oldValues, newValue);
callback(success);
}