what is GlideAjax ?How to write Ajax function?

Lets start first try to understand what is AJAX in general before we jump in ServiceNow GlideAjax .

AJAX term is stand for Asynchronous JavaScript and XML. AJAX allows us  to update part of our web page asynchronously by exchanging small amounts of data with the server, that’s exchange occurs behind the scene instead of reloading the whole form.


Source http://www.w3schools.com/php/php_ajax_intro.asp

The GlideAjax allows you to trigger server-side code from the client side. the main benefit of GlideAjax that you can easily calls pass parameters to the script includes and using naming conventions, allows the use of these parameters.
Lets take an example:
as you properly know that in ServiceNow, the relation between Company and Location is many to many.lets say that you would like to do the following:
– when a user selects a company, the location field should be populated with first retrieved location

in order to do that, you need in the beginning to create new script include and the do the folloing:

1- set Client callable to true

2- set the name field to be exactly the same as the class name.

3-  lets write the in the script, code like below

var LocationAjax = Class.create(); // create Class
LocationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ajaxFunction_LocationAjax : function() {  // public method
var customerID = this.getParameter(‘sysparm_custid’); //paramter from the client side
var LocDataArr = this._getLocationForA(customerID );
return LocDataArr; // return one element
_getLocationForA: function(CID)// private method
var Locationlist=”;
var ag = new GlideRecord(‘cmn_location’);
ag.addQuery(‘company’, CID);
while (ag.next()) {
Locationlist = ag.sys_id;
return Locationlist;
// Class Name
type : “LocationAjax”


4- in the client side
// get sysID for the company
var customerNo = g_form.getValue(‘u_company’);
var gajax = new GlideAjax(“MyFavoritesAjax”); // call the Script Include class
// call the public method
gajax.addParam(“sysparm_name”, “ajaxFunction_getFavorites”);
// pass the sysID to the method
gajax.addParam(“sysparm_custid”, customerNo);
//callback function is used to return the result, passed to the getXML function
function callBackLocation(response) {
var answer = response.responseXML.documentElement.getAttribute(“answer”);
if (null != answer){
g_form.setValue(‘location’, answer);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s