
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2023 11:11 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:14 AM - edited 07-05-2023 10:20 AM
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
Vicks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2023 12:30 AM
Hello Peter,
No. You don't need the ga part at all. The resource script is running tied to your REST request. It is automatically executed. Your respones and its body is already there. Your response body IS response.body
Feel free to go through the doc i've linked. It really details this well.
Regards
Fabian
ps.: If i misunderstood, sorry. Maybe you could shortly explain the sorrounding setting and what the REST resource should do? Thanks 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 07:20 AM
So basically we have a client callable include script that extends Ajax that we use on incidents as well as in a record producer that does specific filtering and assigns an assignment group based on certain conditions. We also have a REST script that I want to use that same client callable filtering code because it too also creates incidents. It doesn't appear I can lift the code I am using on the incident form (onChange value) and place it in the rest script. So this would be an example of how I am calling it our Record Producer as well as an OnChange value on the incident form:
var myResponse = "";
function GetResponse(response)
{
gs.log("Response function called");
myResponse= response;
}
var s_inc = new GlideAjax('GetAssignmentGroup');
s_inc.addParam('sysparm_name', 'getAssignmentGroupCondition1');
s_inc.addParam('sysparm_parm1', val1);
s_inc.addParam('sysparm_parm2', val2);
s_inc.getXMLAnswer(GetResponse);
How would I reformulate that to work in a REST script? I already know that calling the function with new GlideAjax definitely won't work in the REST rest script. It also sounds like getXMLAnswer is not going to work as well?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 08:04 AM
Hey,
so this really depends on how your script include looks...
GlideAjax functions use "this.getParameter()" to get passed parameters from the client side. Usually, script includes using glide ajax look like this:
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld:function() { return "Hello " + this.getParameter('sysparm_user_name') + "!"; } ,
_privateFunction: function() { // this function is not client callable
}
});
Notice, that the helloWorld:function does not have any function parameters?
Now what i like to do when writing scripts is to always extract the actual business logic into non-client-callable functions:
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld:function() {
var userName = this.getParameter('sys_parm_user_name');
return _helloWorlds(userName);
} ,
_helloWorld: function(userName) {
return "Hello " + userName + "!";
}
});
What this allows me to do is call the "_helloWorld" functino from anywhere as if it was just a normal script include (so "new HelloWorld()._helloWorld(userName)"). If, however it is coded as in the first example (all the code in the helloWorld function), then it only runs, if it has access to some GlideAjax parameter (which it won't if you call it server side).
Now i don't know how your code is structured, but this will guide you, wether you can call a private function or not. If not, you can extract the business logic and create a new, private function yourself.
Hope this helps.
Regards
Fabian

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 08:28 AM - edited 07-05-2023 10:10 AM
Okay we are getting closer. It is called like the latter in your reply
var GetAssignmentGroup = Class.create();
GetAssignmentGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignmentGroupCondition1: function() {
var results = [];
var parm1 = this.getParameter('sysparm_parm1');
var parm2 = this.getParameter('sysparm_parm2');
so sounds like I need to call it like this?
var response = GetAssignmentGroup().getAssignmentGroupCondition1(value1, value2);
How do you handle multiple parameters so it gets assigned to the correct value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:14 AM - edited 07-05-2023 10:20 AM
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
Vicks