When to use GlideAjax
Use GlideAjax when a Client Script needs data that is only available on the server — like querying a related table, checking a user's group membership, or fetching a value from a reference field that isn't on the current form.
Do not use GlideAjax for simple display logic that can be done client-side with data already on the form.
Step 1: Create the server-side Script Include
The Script Include must be client callable and extend AbstractAjaxProcessor:
var GetUserGroups = Class.create();
GetUserGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroups: function() {
var userId = this.getParameter('sysparm_user_id');
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userId);
gr.query();
while (gr.next()) {
groups.push(gr.group.getDisplayValue());
}
return JSON.stringify(groups);
},
type: 'GetUserGroups'
});
Step 2: Write the Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;
var ga = new GlideAjax('GetUserGroups');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_user_id', newValue);
ga.getXMLAnswer(function(response) {
var groups = JSON.parse(response);
console.log('User groups: ' + groups.join(', '));
});
}
Synchronous vs asynchronous calls
Use getXMLAnswer(callback) for asynchronous calls — always prefer this. The callback receives the return value from your Script Include method as a string.
Avoid getXML() and synchronous patterns — they block the browser UI and are deprecated.
Passing multiple parameters
ga.addParam('sysparm_name', 'myMethod');
ga.addParam('sysparm_param1', value1);
ga.addParam('sysparm_param2', value2);
On the server side, retrieve with this.getParameter('sysparm_param1').
Returning complex data
Return values must be strings. Use JSON.stringify() for complex objects and JSON.parse() in the callback to deserialise:
// Server: return JSON.stringify({count: 5, items: [...]});
// Client: var data = JSON.parse(response);
Error handling
Always validate parameters on the server before using them. If a parameter is missing or invalid, return a JSON object with an error field rather than throwing an exception, which gives the client a way to handle it gracefully.