Can't get Glide Ajax function to work in Scripted Rest Resource

peterscaramuzzo
Tera Expert

I have a script include that I want to call inside a Scripted REST Resource. However if I call the function using:

 

new GlideAjax('MyFunction')

 

I get syntax errors. So I have to change the call in the code to

 

new MyFunction();

 

This works, however I am now noticing that my call to getXMLAnswer never gets called. It never executes my gs.log command, that I placed inside that getXMLAnswer call. Is it not working because I need to call the function as

 

new GlideAjax('MyFunction')

 

to get that to work? If so, how do I get around that error that causes the whole script to fail when I call it in this way? 

 

1 ACCEPTED SOLUTION

Hi,

 

you might update your existing script include to add below lines in order to accept parameters from Server side scripting.

 

//Script include

 

 

var GetAssignmentGroup = Class.create();
GetAssignmentGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignmentGroupCondition1: function(parm1,parm2 ) { // adding parameters in order to access values from server side scripting
var results = [];
if( !parm1 && !parm2 ){ // if parm1 and parm 2 are null then the script include is probably being called from client script; hence if statement
var parm1 = this.getParameter('sysparm_parm1');
var parm2 = this.getParameter('sysparm_parm2');
}
//evaluate parm1 and parm2 as you need it

 

 

 

//REST Script

 

 

// your code

var script_include_result = new GetAssignmentGroup().getAssignmentGroupCondition1("<parm1_value>","<parm2_value>")

// use above result as you need it

 

 

 

Hope this helps.

 

Regards,

Vicks

 

 

Regards,
Vicks

View solution in original post

12 REPLIES 12

This helped a lot!!!! Thank you!!!! 

Hey,

Great solution! Just one small best-practice thing. Whenever you are coding GlideAjax script includes with server-side and client-callable functions, try to distinguish between them when naming functions. It increases reusability and adds additional error handling capabilities:

 

NOTE: this is the same code, but with sticking to those best practices

var GetAssignmentGroup = Class.create();
GetAssignmentGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getAssignmentGroupCondition: function(){ //this is the client callable one
var parm1 = this.getParameter('sysparm_parm1');
var parm2 = this.getParameter('sysparm_parm2');
if(!param1 && !param2)
  return;

return _getAssignmentGroupCondition(param1, param2);
}

_getAssignmentGroupCondition: function(parm1,parm2 ) { // this is the non-client callable function
var results = [];
if( !parm1 && !parm2 )
return;

//do stuff
return results;
}

Riya Verma
Kilo Sage
Kilo Sage

Hi @peterscaramuzzo ,

 

Hope you are doing great.

 

When using a Scripted REST Resource in ServiceNow to call a script include function, you may encounter syntax errors when using new GlideAjax('MyFunction'). To address this issue, you can modify your code and use new MyFunction() instead..

 

So , you can store the response from script directly in a variable by below syntax:

 

var resposne = new <script include name>().<function_name>()

 

Its not required to use the Glide Ajax in scripted rest resource as Glide Ajax is used for calling server sise function in client side. 

You cane use directly above synatx and store it a variable , in this way there is no need to have GetXMLAnswer() too as its GlideAjax API function.

 

 

 

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma