GlideAjax and Script Include

sanketkhot7
Tera Contributor

Hi everyone,

I want to understand GlideAjax and Script Includes in ServiceNow from basics to advanced level.

Could someone please explain the following concepts with real-time examples and step-by-step code explanations?

  1. GlideAjax: What is GlideAjax, why do we use it, and what are its methods, such as addParam(), getXML(), and getXMLAnswer()?

  2. Script Include: What is a Script Include, why was it introduced when we already have Business Rules, and what are its types and methods?

  3. AbstractAjaxProcessor: What is it, why do we extend it in a client-callable Script Include, and how does it work?

  4. Callback Function: What is a callback function, why is it required with asynchronous GlideAjax calls, and how does it work?

  5. Synchronous vs. Asynchronous: What is the difference, and how does asynchronous execution work in GlideAjax?

  6. Parameters: What is the purpose of sysparm_name, sysparm_type, sysparm_value, and custom parameters such as sysparm_caller_id? How do we retrieve them using this.getParameter()?

  7. Execution Flow: How does a request travel from a Client Script to GlideAjax, then to a Script Include, and finally return a response to the Client Script?

  8. Business Rule vs. Script Include: What is the difference, when should we use each, and can a Script Include be called from a Business Rule or other server-side scripts?

  9. Practical Example: Please explain a real-time scenario, such as retrieving a caller's department when the Caller field changes on an Incident form, with complete code for both the Client Script and Script Include.

I want to understand not only the syntax but also why, when, and how each method is used, with simple explanations and practical examples.

Thank you!

7 REPLIES 7

Aditya_hublikar
Giga Sage

Hello @sanketkhot7 ,

 

A Script Include in ServiceNow is a reusable piece of server-side JavaScript code. Instead of copying and pasting the same logic across multiple Business Rules, UI Actions, or Workflows, you centralize it inside a Script Include and call it wherever it is needed.

 

There are 3 types :

1) Creating new class 

2)Extending existing class i.e  GlideAjax

3)On demand/classless 

 
 
 
GlideAjax is a built-in ServiceNow API class that enables client-side scripts (running in the user's browser) to securely execute server-side code (running on the database)
 

It provides methods such as addParam(), getXML(), and getXMLAnswer() to send parameters and retrieve responses asynchronously. A Script Include stores reusable server-side JavaScript logic and can be called from Client Scripts, Business Rules, and other server-side scripts. For client-side GlideAjax requests, we typically extend AbstractAjaxProcessor in a client-callable Script Include. A callback function processes the server response when it arrives. For example, when the Caller field changes on an Incident form, GlideAjax can call a Script Include to retrieve the caller's department and display it on the form. Unlike Script Includes, Business Rules execute automatically when their configured database conditions are met.

 

For more details and example refer this :

Mastering script include

GlideAjax Cheatsheet

 

 

If this helps you then mark it as helpful and  accept as solution.

Regards,

Aditya

 

Bhavya11
Mega Patron

Hi @sanketkhot7 ,

 

Kindly go through this Scripting in ServiceNow Fundamentals (Australia)  leanring

 


Regards
BK
LinkedIn: https://www.linkedin.com/in/bhavyashree-karanth-55032a121
YouTube: https://www.youtube.com/@NowWithBhavya

tarunrekand
Tera Contributor

Hi @sanketkhot7 ,

 

  1. GlideAjax:GlideAjax is the standard ServiceNow client-side API used to execute server-side code within a Script Include and pass the results back to the browser asynchronously.
    1. addParam():addParam() is used to call the function and pass the data from a client script to a server-side script.
    2. getXML():getXML() retrieves the full XML document.
    3. getXMLAnswer():getXMLAnswer()retrieves the raw string data.
  2. Script Include: A Script Include is a reusable server-side script. It can be called in a Client Script using GlideAjax, and used in a Business Rule by instantiating it as an object.
  3. AbstractAjaxProcessor: AbstractAjaxProcessor acts as the bridge between the browser and the database.
  4. GlideAjax: JavaScript function that processes the server-side response asynchronously after a request is sent from the client-side.
  5. Synchronous vs. Asynchronous: Synchronous and Asynchronous execution dictates whether a script waits for a process to finish before moving on, or lets it run in the background.
  6. Parameters: parameters are variables used to pass data between scripts.
  7. Business Rule vs. Script Include: 
    • Business Rules are triggers that automatically execute server-side logic when a record is created, updated, or deleted.
    • Script Includes are reusable Functions that store server-side code to be called on-demand by other scripts.
  8. Practical Example: 
    1. onChange Client Script(): 

      function onChange(control, oldValue, newValue, isLoading, isTemplate) {

          if (isLoading) {
              return;
           }
           if (!newValue) {
               g_form.clearValue('u_caller_department');
               return;
            }


      var ga = new GlideAjax('CallerDepartmentAjax');
      ga.addParam('sysparm_name', 'getDepartment');
      ga.addParam('sysparm_caller_id', newValue);
      ga.getXMLAnswer(function(answer) {
      g_form.setValue('u_caller_department', answer || '');

      });
      }

    2. Script Include:

      var CallerDepartmentAjax = Class.create();

      CallerDepartmentAjax.prototype = Object.extendsObject(
      AbstractAjaxProcessor, {

      getDepartment: function() {
      var callerId = this.getParameter('sysparm_caller_id');

      if (!callerId) {
      return '';
      }

      var userGR = new GlideRecord('sys_user');

      if (userGR.get(callerId)) {
      var department = userGR.getValue('department');

      return department || '';
      }

      return '';
      },

      type: 'CallerDepartmentAjax'
      }
      );

Regards,
Tarun