GlideAjax is a class used in the ServiceNow Platform to allow client side javascript access to server side
functions, classes, and methods.
GlideAjax seems more verbose then GlideRecord but it is a also a much better use of network
traffic. GlideRecord pulls all the things related to a single or multiple records. GlideAjax only returns
what you define.
Because most avoid GlideAjax calls I always refer to an example
I wrote to define the server side code and I'll share a cut down version of that here;
// Script Include
var SomeUtil = Class.create();
SomeUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Do not use an initialize method here
awesomeFunction: function(){
var inputObj = JSON.parse(this.getParameter('sysparm_obj'));
var returnObj = {
from:"server",
input: inputObj
};
return JSON.stringify(returnObj);
},
type: 'SomeUtil'
});
Once this is defined you can call it from any page where GlideAjax is defined with code like this;
// Client Side Code
var ga = new GlideAjax('global.SomeUtil');
ga.addParam('sysparm_name', 'awesomeFunction');
ga.addParam('sysparm_obj', JSON.stringify({"hoo":"raa"}));
ga.getXML(function(response){
var responseDocument = response.responseXML.documentElement;
var answer = responseDocument.getAttribute('answer');
var serverObj = JSON.parse(answer);
console.log(serverObj);
});
Below I'll go over each of this classes methods.
Specifies a parameter name and value to be passed to the server-side function associated with this GlideAjax object.
The first call to addParam should be with the parameter sysparm_name and the name of the server-side method you want to call. The server-side code does not execute until the client script calls getXML().
// ...
ga.addParam('sysparm_name', 'awesomeFunction');
ga.addParam('sysparm_color', 'red');
Sends the server a request to execute the method and parameters associated with this GlideAjax object.
The server processes the request asynchronously and -- when ready -- returns the results via the function specified as the callback_function.
// as a nested function
ga.getXML(function(response){
var responseDocument = response.responseXML.documentElement;
var answer = responseDocument.getAttribute('answer');
var serverObj = JSON.parse(answer);
console.log(serverObj);
});
// as a declared function
function handleResponse(response){
var responseDocument = response.responseXML.documentElement;
var answer = responseDocument.getAttribute('answer');
var serverObj = JSON.parse(answer);
console.log(serverObj);
}
ga.getXML(handleResponse);
Call the processor asynchronously and get the answer element of the response in XML format.
This is just shorthand for getXML.
// as a nested function
ga.getXMLAnswer(function(response){
var serverObj = JSON.parse(response);
console.log(serverObj);
});
This method can still be used in Servicenow but only in UI16. As it's not recommeneded to block the
execution of code, please avoid using this. Also this won't work on mobile, service portal or scoped applications.
Sends the server a request to execute the method and parameters associated with this GlideAjax object.
The server processes the request synchronously and will not process further requests from the client until finished. To retrieve the results, the client must call getAnswer(). Using getXMLWait() ensures the order of execution, but can cause the application to seem unresponsive, significantly degrading the user experience of any application that uses it. We recommend using getXML() instead.
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name',"Bob");
ga.getXMLWait();
alert(ga.getAnswer());
All the documentation in this page is taken from the following sources;