ServiceNow Client Script and GlideAjax

In ServiceNow, Client Scripts and GlideAjax are essential tools for creating a dynamic user experience and interacting with server-side logic from the client side.

Client Script in ServiceNow

As the name suggests– “Scripts for Client Side”. It is a piece of JavaScript that does not run on the server but runs in the user’s web browser. The script helps in enhancing the user experience by adding some informative, warning or error messages and by configuring the populating values, visibility, mandatory and read-only features on the form.  We must provide the table name on which table it needs to be run.

Form looks like:

Client script is classified on based of class. The other one is catalog client script that is only used for scripting in record producer and service catalog forms configurations. The only difference we have is to provide the catalog item name or variable set name where the script needs to be attached.

Form looks like:

There are some types of Client Scripts that are used for different purpose:
  1. OnLoad: This script runs whenever the form is loaded. It helps in configuring the form layout before the user starts interacting on the form by enhancing visibility, readability or populating values.
  2. OnChange: These scripts run when a specific field value is modified. This is useful when the field value is changed, and new field value needs to have a particular action like new fields need to be visible or some info message.
  3. OnSubmit: These scripts triggers when the form gets submitted. This can be used to check the validation of the form, the correct data is filled with proper validations. This helps in preventing the form from invalid data.
  4. OnCellEdit: This script executes on list edit, when user tries to edit the field from list.
Ways to Communicate with Server Side: As this is client-side script and can’t be used in server side, but In some scenarios if we required to fetch the data from server side we have to use “GlideAjax” or “getReference”.
getReference()- The getReference() function in ServiceNow works in a similar way to GlideRecord() that fetches referenced field on form. It is best to use when single detail from related record needs to be retrieve. Here’s a overview of how to use:
  • It’s better to use asynchronous approach for avoiding blockers from browser.
Best Practices for “getReference()”
  • Asynchronous Script: Better to use asynchronous approach to avoid blocking the browser.
  • Minimizing Server Usage: Using g_form.getReference() to have better performances.

GlideAjax in ServiceNow

GlideAjax is object in client script that is used to communicate with the server-side script that is script include. In GlideAjax by using OOB function it becomes easy to communicate with the server side.

It is a feature in ServiceNow that allows for asynchronous calls to server-side scripts from client-side scripts. This is useful while data needs to be retrieved from server-side, performing some calculations or building a logic on the form.

How to Use GlideAjax
  1. Create a Script Include:
    • This script include should include the AbstractAjaxProcessor class and should be public script include. To include the AbstractAjaxProcessor we need to check the box for client callable. If the script is client callable then only the script can be called in client.

2. Create a client Script that includes the script include that created.

Best Practices for “GlideAjax”
  • Descriptive Names: Name of your script include and function name should be descriptive so that it explains what exactly it contains.
  • Parameter Prefix: Using sysparm_ prefix as parameters.
  • Error Handling: Using try/catch blocks or log() for debugging the script.
Performance Considerations: Using GlideAjax instead of getReference() can increase the performance

When to Use Client Scripts and GlideAjax in ServiceNow

  • Use Client Scripts for immediate, user-facing logic that doesn’t need server data or involves simple field manipulations.
  • Use GlideAjax when you need data from the server or want to execute complex business logic that should remain server-side for security and performance reasons.
Together, Client Scripts and GlideAjax create a powerful combination for developing interactive, efficient ServiceNow applications that ensure both usability and secure access to data.