Scripting
What is a GlideAjax call, and why do you need it?
The question
What This Question Really Tests
This is a top-tier beginner question because it tests understanding of the client-server boundary — the most important architectural concept in ServiceNow development.
Why You Need GlideAjax
Client Scripts run in the browser. They can manipulate the form with g_form, but they cannot query the database directly. GlideRecord is a server-side-only API. So when a Client Script needs data from the database, it must ask the server — and GlideAjax is the mechanism for that request.
Think of it as making an AJAX call to a backend API endpoint, except the 'endpoint' is a Script Include method.
Step-by-Step Example
// ============================================
// STEP 1: Create the Script Include (server-side)
// Name: UserInfoHelper
// Accessible from: All application scopes
// ============================================
var UserInfoHelper = Class.create();
UserInfoHelper.prototype = Object.extendsObject(
AbstractAjaxProcessor, { // MUST extend this for GlideAjax
// This method will be callable from the client
getUserDepartment: function() {
var userId = this.getParameter('sysparm_user_id');
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) {
return gr.getDisplayValue('department');
}
return 'Unknown';
},
type: 'UserInfoHelper'
});
// ============================================
// STEP 2: Create the Client Script (client-side)
// Type: onChange
// Field: caller_id
// ============================================
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;
// Create the GlideAjax call
var ga = new GlideAjax('UserInfoHelper'); // Script Include name
ga.addParam('sysparm_name', 'getUserDepartment'); // Method to call
ga.addParam('sysparm_user_id', newValue); // Pass the user's sys_id
// Make the call (asynchronous — doesn't freeze the form)
ga.getXMLAnswer(function(answer) {
// 'answer' contains the return value from the server
g_form.setValue('u_department', answer);
});
}How It Works
- Client Script creates a GlideAjax object pointing to the Script Include name
- Passes the method name via
sysparm_nameparameter - Passes any custom data via additional
sysparm_parameters - Calls
getXMLAnswer()with a callback function — this is asynchronous - ServiceNow server receives the request, instantiates the Script Include, calls the method, and returns the result
- The callback function receives the result and updates the form
Important Rules
- The Script Include must extend AbstractAjaxProcessor to be callable via GlideAjax
- Use
this.getParameter('sysparm_xxx')to read parameters on the server side - Always use the asynchronous pattern (
getXMLAnswer) — the synchronous version (getXMLWait) freezes the browser - Return values are always strings — if you need objects, use
JSON.stringify()andJSON.parse()
💡 Interview Tip
If asked to whiteboard a GlideAjax call, always include: (1) the Script Include extending AbstractAjaxProcessor, (2) the Client Script with sysparm_name, and (3) the asynchronous callback. Missing any of these three pieces signals you've only read about it, not used it.