GlideAjax (Client-Server Communication)
GlideAjax is a key mechanism in ServiceNow that enables asynchronous communication between client-side scripts (such as Client Scripts or UI Policies) and server-side Script Includes. Since client-side scripts cannot directly access server-side APIs like GlideRecord for security and performance rea… GlideAjax is a key mechanism in ServiceNow that enables asynchronous communication between client-side scripts (such as Client Scripts or UI Policies) and server-side Script Includes. Since client-side scripts cannot directly access server-side APIs like GlideRecord for security and performance reasons, GlideAjax acts as a bridge to facilitate this communication. **How It Works:** 1. **Client-Side Call:** A client script creates a GlideAjax object, referencing a Script Include by name. Parameters are passed using the `addParam()` method. The special parameter `sysparm_name` specifies which server-side function to invoke. 2. **Server-Side Processing:** The corresponding Script Include must extend `AbstractAjaxProcessor`. The specified function executes server-side logic (e.g., querying records, performing calculations) and returns data using `this.newItem()` or by setting attributes on the response. 3. **Callback Response:** The client script receives the response asynchronously through a callback function, ensuring the user interface remains responsive without full page reloads. **Example:** Client Script: ```javascript var ga = new GlideAjax('MyScriptInclude'); ga.addParam('sysparm_name', 'getDetails'); ga.addParam('sysparm_user_id', g_form.getValue('caller_id')); ga.getXMLAnswer(function(response) { g_form.setValue('description', response); }); ``` Script Include: ```javascript var MyScriptInclude = Class.create(); MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, { getDetails: function() { var userID = this.getParameter('sysparm_user_id'); var gr = new GlideRecord('sys_user'); if (gr.get(userID)) { return gr.getValue('email'); } return ''; } }); ``` **Key Best Practices:** - Always use asynchronous calls (`getXMLAnswer`) rather than synchronous (`getXMLWait`) to avoid freezing the UI. - Mark the Script Include as **Client Callable** to make it accessible from GlideAjax. - Minimize the data transferred between client and server for optimal performance. - Validate inputs on the server side to prevent security vulnerabilities. GlideAjax is essential for building dynamic, responsive ServiceNow applications while maintaining proper separation between client and server logic.
GlideAjax (Client-Server Communication) – Complete Guide for ServiceNow CAD Exam
Why GlideAjax Is Important
In ServiceNow, there is a strict separation between the client-side (the user's browser) and the server-side (the ServiceNow instance). Client-side scripts such as Client Scripts and UI Policies run in the browser and have limited access to server-side data and logic. When you need to retrieve data from the server, perform complex queries, or execute server-side business logic from a client-side context, you need a mechanism to bridge that gap. GlideAjax is that mechanism.
Without GlideAjax, developers might resort to using synchronous GlideRecord calls on the client side (using getXMLWait()), which blocks the user interface, degrades performance, and creates a poor user experience. GlideAjax enables asynchronous communication, meaning the browser does not freeze while waiting for a server response. This is a best practice heavily emphasized by ServiceNow and frequently tested on the Certified Application Developer (CAD) exam.
What Is GlideAjax?
GlideAjax is a client-side JavaScript class that allows client scripts to call Script Includes on the server asynchronously. It works as follows:
1. A client-side script (Client Script, UI Policy script, UI Action with client-side code) creates a GlideAjax object, passing the name of a Script Include as a parameter.
2. The client script sets parameters (name-value pairs) that will be sent to the server.
3. The client script calls getXMLAnswer() (or getXML()) with a callback function.
4. The server executes the specified Script Include method and returns a result.
5. The callback function on the client side processes the returned value.
The Script Include that GlideAjax calls must extend AbstractAjaxProcessor. This is a critical requirement.
How GlideAjax Works – Step by Step
Step 1: Create the Script Include (Server-Side)
You create a Script Include that extends AbstractAjaxProcessor. The Script Include must be marked as Client Callable (the checkbox must be checked). Inside, you define functions that the client can invoke. Each function uses this.getParameter() to retrieve parameters passed from the client and returns a value using return.
Example Script Include named UserUtils:
var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmail: function() {
var userID = this.getParameter('sysparm_user_id');
var gr = new GlideRecord('sys_user');
if (gr.get(userID)) {
return gr.getValue('email');
}
return '';
},
type: 'UserUtils'
});
Step 2: Create the Client Script (Client-Side)
In a Client Script (typically an onChange or onLoad script), you create a GlideAjax object and specify the Script Include name, the method to call (via sysparm_name), and any additional parameters (using custom sysparm names).
Example Client Script:
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getUserEmail');
ga.addParam('sysparm_user_id', g_form.getValue('caller_id'));
ga.getXMLAnswer(function(answer) {
g_form.setValue('email', answer);
});
Key Details of the Communication Flow:
- new GlideAjax('UserUtils') – Creates the GlideAjax object and specifies the Script Include to call.
- ga.addParam('sysparm_name', 'getUserEmail') – Tells the server which function inside the Script Include to execute. The parameter name sysparm_name is mandatory and reserved for this purpose.
- ga.addParam('sysparm_user_id', value) – Passes custom parameters. All custom parameter names should start with sysparm_ by convention.
- ga.getXMLAnswer(callback) – Sends the asynchronous request to the server and processes the plain-text response in the callback function. The answer parameter in the callback contains the return value from the Script Include function.
getXMLAnswer() vs getXML()
- getXMLAnswer(callback) – Returns the answer directly as a string. This is simpler and is the most commonly used method.
- getXML(callback) – Returns the full XML document. You would need to parse it using response.responseXML.documentElement.getAttribute('answer'). This is more complex but allows you to return multiple values using this.newItem() in the Script Include.
Returning Multiple Values
If you need to return multiple values from the server, use getXML() instead of getXMLAnswer(). In the Script Include, use this.newItem('itemName') to create XML items with attributes:
Server-side (Script Include):
getMultipleValues: function() {
var userID = this.getParameter('sysparm_user_id');
var gr = new GlideRecord('sys_user');
if (gr.get(userID)) {
var item = this.newItem();
item.setAttribute('email', gr.getValue('email'));
item.setAttribute('phone', gr.getValue('phone'));
}
}
Client-side:
ga.getXML(function(response) {
var items = response.responseXML.getElementsByTagName('item');
if (items.length > 0) {
var email = items[0].getAttribute('email');
var phone = items[0].getAttribute('phone');
}
});
Common Mistakes and Pitfalls
- Forgetting to check 'Client Callable' on the Script Include. Without this, the GlideAjax call will fail.
- Not extending AbstractAjaxProcessor. The Script Include must extend this class, not just be a regular Script Include.
- Using synchronous calls (getXMLWait) instead of asynchronous (getXMLAnswer/getXML). Synchronous calls block the browser and are strongly discouraged.
- Forgetting sysparm_name. This parameter is mandatory and tells the server which method to call.
- Trying to use GlideRecord on the client side. While possible, this is bad practice. GlideAjax is the recommended approach for retrieving server data from client scripts.
- Placing server-side code in client scripts. Remember that GlideAjax separates concerns — the server logic belongs in the Script Include, not the client script.
When to Use GlideAjax
Use GlideAjax when you need to:
- Query database records from a client script
- Execute complex server-side logic triggered by a client-side event
- Validate data against server-side records before form submission
- Retrieve calculated or aggregated values from the server
- Avoid performance issues caused by synchronous server calls
GlideAjax vs Other Approaches
- GlideAjax – Asynchronous, best practice, recommended approach for client-to-server communication.
- Client-side GlideRecord (getXMLWait) – Synchronous, blocks the UI, poor practice, should be avoided.
- Client-side GlideRecord (addQuery + query with callback) – Asynchronous but exposes server-side query logic to the client. Less secure and less reusable than GlideAjax with Script Includes.
- g_scratchpad – Used in Display Business Rules to pass data to the client on form load. Cannot be used dynamically after the form loads. Good for initial data but not for responding to user actions.
- GlideForm (g_form) getReference() – Can retrieve a referenced record asynchronously, but is limited to reference fields and returns the entire record. Less flexible than GlideAjax.
Security Considerations
Since Script Includes marked as Client Callable can be invoked from the browser, they can potentially be called by any authenticated user. Always validate input parameters, enforce ACLs, and avoid exposing sensitive operations through client-callable Script Includes. Consider checking user roles within the Script Include if needed.
Exam Tips: Answering Questions on GlideAjax (Client-Server Communication)
1. Know the Required Components: When an exam question asks about implementing GlideAjax, remember the two required components: a Client Script (or UI Action/UI Policy script) on the client side and a Script Include extending AbstractAjaxProcessor with Client Callable checked on the server side.
2. Always Choose Asynchronous: If an exam question presents options between synchronous and asynchronous approaches, always choose the asynchronous GlideAjax approach (getXMLAnswer or getXML). ServiceNow best practices strongly favor asynchronous communication. If you see getXMLWait(), that is almost always the wrong answer.
3. Remember sysparm_name: The parameter sysparm_name is required and specifies which function in the Script Include to execute. If an exam question shows code missing this parameter, that code is incorrect.
4. Understand the Callback Pattern: Questions may test whether you understand that GlideAjax is asynchronous. Code that appears after getXMLAnswer() but outside the callback function will execute before the server response arrives. Any logic that depends on the server response must be inside the callback function.
5. Distinguish getXMLAnswer from getXML: getXMLAnswer() returns a simple string. getXML() returns an XML document. If the question involves returning a single value, getXMLAnswer() is appropriate. If multiple values need to be returned, getXML() with newItem() is the correct approach.
6. Know When NOT to Use GlideAjax: If data is needed only at form load time and does not change based on user interaction, a Display Business Rule with g_scratchpad may be more appropriate. If the question specifically says data is needed on form load and does not change, g_scratchpad could be the better answer.
7. Identify Incorrect Code Patterns: Watch for these red flags in exam answers:
- Script Include that does NOT extend AbstractAjaxProcessor
- Script Include that is NOT marked as Client Callable
- Using this.getParameter() in a client script (it belongs on the server side)
- Using ga.addParam() without the sysparm_name parameter
- Processing the GlideAjax response outside of the callback function
8. Understand the Execution Context: GlideAjax Script Includes run on the server. Inside them, you have full access to server-side APIs like GlideRecord, GlideSystem (gs), and GlideDateTime. On the client side, you only have access to client-side APIs like g_form, g_user, and GlideAjax itself.
9. Practice Reading Code Snippets: Many CAD exam questions present code and ask you to identify what is wrong or what the output will be. Practice reading GlideAjax code and tracing the flow from client to server and back.
10. Remember the Naming Convention: Custom parameters should use the sysparm_ prefix. While this is a convention rather than a strict requirement, exam questions typically follow this pattern, and it helps you identify which parameters are custom versus system-defined.
Summary Checklist for GlideAjax:
✓ Script Include extends AbstractAjaxProcessor
✓ Script Include is marked Client Callable
✓ Client script creates new GlideAjax('ScriptIncludeName')
✓ Client script adds sysparm_name parameter with the function name
✓ Client script adds any custom sysparm_ parameters
✓ Client script uses getXMLAnswer() or getXML() with a callback
✓ All response-dependent logic is inside the callback function
✓ Server-side function uses this.getParameter() to retrieve values
✓ Server-side function returns a value (for getXMLAnswer) or uses newItem() (for getXML)
🎓 Unlock Premium Access
ServiceNow Certified Application Developer + ALL Certifications
- 🎓 Access to ALL Certifications: Study for any certification on our platform with one subscription
- 3305 Superior-grade ServiceNow Certified Application Developer practice questions
- Unlimited practice tests across all certifications
- Detailed explanations for every question
- CAD: 5 full exams plus all other certification exams
- 100% Satisfaction Guaranteed: Full refund if unsatisfied
- Risk-Free: 7-day free trial with all premium features!