- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
05-19-2022 11:37 AM - edited 10-08-2025 02:51 AM
Maybe you don't know it, but it is actually possible to have a initialize function for client callable script includes. The default-initialize vanishes once you hit the 'Client callable' checkbox, but you can add this function to make it work again:
initialize: function(request, responseXML, gc) {
global.AbstractAjaxProcessor.prototype.initialize.apply(this, arguments);
this.myVar = 'myValue';
}Keep in mind thought that it is not possible to use any (meaningfull) parameters for your Script Include calls (how would you even be able to transmit those in a GlideAjax call right?). If you had a regular client callable script include that you were already using server side as well, then you wouldn't have had any initialize-parameters anyways...
Full example:
/* global global, Class */
/* eslint no-undef: "error" */
var MYSCRIPTINCLUDE = Class.create();
MYSCRIPTINCLUDE.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
initialize: function(request, responseXML, gc) {
global.AbstractAjaxProcessor.prototype.initialize.apply(this, arguments);
this.myArg = 'myValue';
},
// the result of this call will be casted to string anyways,
// therefore we use a JSON for your result object
MYFUNC: function() {
var param = this.getParameter('MYPARAM');
return JSON.stringify({
param: param + this.myArg // echo just for demonstration purposes
});
},
type: 'MYSCRIPTINCLUDE'
});
Appendix: Properly Debugging Ajax / Client Callable Script Includes
How to debug a failing GlideAjax call? You will not get even get a error log and debugging the underlying Script Include using the Script Debugger is not possible, because breakpoints will not trigger by when initiated by a GlideAjax call.
But did you know that the Script Include can actually be called using a Background Script? Here all breakpoints are trigger just fine, the only thing missing is parameter handling.
Take this GlideAjax call for example:
var ga = new GlideAjax('x_my_scope.SCRIPTINCLUDE');
ga.addParam('sysparm_name', 'MYFUNCTION');
ga.addParam('my_param1', 'MYVALUE1');
ga.addParam('my_param2', 'MYVALUE2');
ga.getXMLAnswer(function (result) {
// do something with result
});This can be transformed to a background script, which then will trigger all the breakpoints in your Client callable Script Include:
var params = {
'my_param1': 'MYVALUE1',
'my_param2': 'MYVALUE2'
};
var instance = new x_my_scope.SCRIPTINCLUDE();
instance.getParameter = function (paramName) { return (paramName in params) ? params[paramName] : null; };
gs.info(instance.MYFUNCTION());The trick is trivial: Overwrite the getParameter of the base class to return values from the params-object instead of the HTTP Request (which is sent by GlideAjax).
- 3,404 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Question: My coding style typically has me doing this.myArg variables as a form of a config variable. So they would never be used on the client side but set up so that I don't have to go through multiple lines of code to change variables. Would this example allow for that?
Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes, as you can see in the "Full Example" above, this is possible.