Unleashing Dynamic Forms in ServiceNow with the Power of GlideAJAX
- nathanlee142
- Mar 20
- 4 min read
Updated: Mar 29

In the world of ServiceNow development, creating user interfaces that respond dynamically to user interactions is often a key requirement. This is where GlideAJAX comes into play, providing a powerful mechanism to bridge the gap between client-side scripts and server-side logic without the need for full page reloads. Understanding the GlideAJAX API is essential for any ServiceNow developer looking to build more interactive and efficient applications. This article will serve as your guide to grasping the fundamentals of GlideAJAX and how you can leverage it to enhance your ServiceNow solutions.
At its core, GlideAJAX enables you to execute server-side scripts, housed within Script Includes, directly from your client-side code, such as Client Scripts or UI Scripts. This communication happens behind the scenes, making your forms more responsive and user-friendly. The process involves a structured exchange of information facilitated by the GlideAjax client-side object and the AbstractAjaxProcessor (which your Script Include inherits).
Think of it like this: your client-side script needs some information or wants to perform an action that requires server-side data or processing. Instead of submitting the entire form and waiting for a response, it can send a quick request to a specific Script Include using GlideAJAX. The Script Include then processes this request and sends back a response, which your client-side script can use to update the user interface.
Here's a breakdown of the key components and their roles in this communication flow:
1. The Request Journey:
Your Client Script Initiates: You start by creating a GlideAjax object in your client-side script, specifying the name of the Script Include you want to call. You then add parameters to this request, essentially telling the Script Include what information you're sending.
GlideAjax the Messenger: The GlideAjax object takes your parameters and constructs an XML request behind the scenes.
AbstractAjaxProcessor the Intermediary: This built-in ServiceNow component receives the XML request and directs it to the Script Include you specified.
Your Script Include Processes: Your custom Script Include receives the request, reads the parameters you sent, performs the necessary server-side logic, and prepares a response.
2. The Response Journey:
Your Script Include Responds: Within your Script Include, you use methods provided by the AbstractAjaxProcessor to format your response, often as XML.
AbstractAjaxProcessor the Return Courier: This component takes the XML response generated by your Script Include.
GlideAjax Receives the Reply: The GlideAjax object on the client-side receives the XML response from the server.
Your Client Script Reacts: Finally, GlideAjax calls a function you defined in your client script, passing the server's response. Your script can then parse this response and update the user interface accordingly.
Key Elements of the GlideAJAX API
Let's delve into the specific methods you'll use:
GlideAjax(scriptIncludeName) (Client-Side): This is the constructor. You create a GlideAjax object by providing the name of the Script Include you wish to interact with. For example:
var ga = new GlideAjax('MyCustomScriptInclude');
addParam(name, value) (Client-Side): This method allows you to send data to your Script Include. You can call it multiple times to add different parameters. For example:
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user_id', 'john.doe');
The sysparm_name parameter is conventionally used to specify the function within your Script Include that you want to execute.
getXML(callbackFunction) (Client-Side): This crucial method sends the asynchronous XML request to the server. The callbackFunction is a function you define that will be executed when the server sends back a response. This function will receive a parameter containing the XML response. For example:
ga.getXML(processUserDetails);
getParameter(paramName) (Server-Side - in your Script Include): Within your Script Include, you use this method (prefixed with this.) to retrieve the parameters you sent from the client-side using addParam(). For example:
var userId = this.getParameter('sysparm_user_id');
newItem(elementName) (Server-Side - in your Script Include): This method, also used with this., creates a new XML element in the response that will be sent back to the client. For example:
var result = this.newItem('result'); // This method returns the XML element object.
setAttribute(name, value) (Server-Side - on the XML Element): Once you've created an XML element using newItem(), you can add attributes to it using this method. For example:
result.setAttribute('userName', 'John Doe'); // This would create an XML element like <result userName="John Doe"></result>.
Accessing the Response (Client-Side - in your callback function): The callbackFunction you passed to getXML() receives an object (often named serverResponse). You can access the XML response using serverResponse.responseXML.
getElementsByTagName(elementName) (Client-Side - on the XML Response): You can use this method on the responseXML object to retrieve an array of XML elements with the specified name that were created in your Script Include using newItem(). For example:
var results = serverResponse.responseXML.getElementsByTagName("result");
getAttribute(name) (Client-Side - on a Client-Side XML Element): After getting an XML element from the response, you can use this method to retrieve the value of a specific attribute you set in your Script Include using setAttribute(). For example:
var userName = results[0].getAttribute("userName");
Illustrative Use Case
Imagine you have a form where a user needs to select a company. You want to dynamically display the main contact person for that company without reloading the entire form. You could achieve this using GlideAJAX:
Client Script (onChange on the Company field):
Create a GlideAjax object, pointing to a Script Include named 'CompanyContactInfo'.
Use addParam() to send the selected company's sys_id to the Script Include.
Call getXML() with a callback function named displayContact.
Script Include ('CompanyContactInfo'):
Create a function (e.g., getContact) that can be called from GlideAJAX.
Inside this function, use this.getParameter() to retrieve the company's sys_id.
Perform a GlideRecord query to find the main contact person for that company.
Use this.newItem() to create an XML element named 'contact'.
Use contact.setAttribute() to add attributes like 'name' and 'email' with the contact person's details.
Client Script (callback function 'displayContact'):
Inside the displayContact function, access the responseXML.
Use getElementsByTagName() to get the 'contact' element.
Use getAttribute() to retrieve the contact's name and email.
Update the appropriate fields on your form with this information.
Conclusion
GlideAJAX is a cornerstone of building interactive and user-friendly ServiceNow applications. By enabling asynchronous communication between the client and server, it allows for dynamic updates and richer user experiences. Understanding the GlideAjax client-side object, the role of the AbstractAjaxProcessor, and the methods for sending requests and processing responses opens up a world of possibilities for enhancing your ServiceNow forms and applications. Take the time to explore and experiment with GlideAJAX to unlock its full potential in your development endeavors.