Debug GlideAjax / client callable script include

In order to debug a GlideAjax Script Include, we have to trigger the code from the server side.

We can run it the same way as we do with any other Script Include: using the background script.

We can simply override the getParameter function defined in AbstractAjaxProcessor, so that it uses some static values instead of retrieving them from the request (that is undefined when we execute the code this way).

var testAjax = new TestAjax();

//Override "getParameter" function from "AbstractAjaxProcessor"
//before you call the function you want to debug
testAjax.getParameter = function(parm){
  var parms = {'sysparm_p1': 'This is the parameter 1',
              'sysparm_p2': 'This is the parameter 2',};
	return parms[parm];
};

testAjax.test();

Code language: JavaScript (javascript)

This is the Script Include containing a simple GlideAjax example.

var TestAjax = Class.create();
TestAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
	//Returns the concatenation of the two parameters
	test: function(){
		var parm1 = this.getParameter('sysparm_p1');
		var parm2 = this.getParameter('sysparm_p2');
		return parm1 + ' - ' + parm2;
	},
    type: 'TestAjax'
});
Code language: JavaScript (javascript)

Why do we need this workaround?

The “initialize” function in “AbstractAjaxProcessor” (a.k.a. GlideAjax) expects a “RequestFacade” object as the first parameter (org.apache.catalina.connector.RequestFacade).

Unluckily, ServiceNow doesn’t allow us to create an instance of that class.

"JavaException: java.lang.SecurityException: Illegal attempt to access class 'org.apache.catalina.connector.RequestFacade' via script"

“getParameter” is the only function from “AbstractAjaxProcessor” that relies on the “request” parameter. So that’s the only one we need to override if we want to debug a GlideAjax script.

// var AbstractAjaxProcessor = Class.create();

AbstractAjaxProcessor.prototype = {

[...]

// returns value of parameter as a Java String instance
    getParameter: function(name) {
        return this.request.getParameter(name)
    },
[...]

};
Code language: JavaScript (javascript)