- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 04:35 AM
Hi all,
I have a script include that contains 3 different functions. I need to use all of them in a catalog client script and am unsure how to call all 3 functions using GlideAjax and also how to get the answer from the response, for each function.
Any help with this would be much appreciated.
Thank you,
G
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:02 AM
Hi @gunishi ,
Yes you can create another function, which calls all the three functions and returns the data of all the three functions in JSON object.
For example see the following Script Include:
var TestMultiFunctions = Class.create();
TestMultiFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAll: function(){
var param1 = this.getParameter("sysparm_param1");
var param2 = this.getParameter("sysparm_param2");
var param3 = this.getParameter("sysparm_param3");
var param4 = this.getParameter("sysparm_param4");
var param5 = this.getParameter("sysparm_param5");
var param6 = this.getParameter("sysparm_param6");
var data = {
functionA: "",
functionB: "",
functionC: "",
};
data.functionA = getFunctionA(param1, param2);
data.functionB = getFunctionA(param3, param4);
data.functionC = getFunctionA(param3, param6);
return JSON.stringify(data);
},
getFunctionA: function(param1, param2){
return "Some data from A";
},
getFunctionB: function(param3, param4){
return "Some data from B";
},
getFunctionC: function(param5, param6){
return "Some data from C";
},
type: 'TestMultiFunctions'
});
Client Script:
function onLoad() {
var ga = new GlideAjax('TestMultiFunctions');
ga.addParam('sysparm_name','getAll');
ga.addParam('sysparm_param1','some_data');
ga.addParam('sysparm_param2','some_data');
ga.addParam('sysparm_param3','some_data');
ga.addParam('sysparm_param4','some_data');
ga.addParam('sysparm_param5','some_data');
ga.addParam('sysparm_param6','some_data');
ga.getXMLAnswer(ProcessData);
}
function ProcessData(response) {
var data = JSON.parse(response);
alert(data.functionA);
alert(data.functionB);
alert(data.functionC);
}
Please mark my answer helpful and accept as solution if it helped you 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:02 AM
Hi @gunishi ,
Yes you can create another function, which calls all the three functions and returns the data of all the three functions in JSON object.
For example see the following Script Include:
var TestMultiFunctions = Class.create();
TestMultiFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAll: function(){
var param1 = this.getParameter("sysparm_param1");
var param2 = this.getParameter("sysparm_param2");
var param3 = this.getParameter("sysparm_param3");
var param4 = this.getParameter("sysparm_param4");
var param5 = this.getParameter("sysparm_param5");
var param6 = this.getParameter("sysparm_param6");
var data = {
functionA: "",
functionB: "",
functionC: "",
};
data.functionA = getFunctionA(param1, param2);
data.functionB = getFunctionA(param3, param4);
data.functionC = getFunctionA(param3, param6);
return JSON.stringify(data);
},
getFunctionA: function(param1, param2){
return "Some data from A";
},
getFunctionB: function(param3, param4){
return "Some data from B";
},
getFunctionC: function(param5, param6){
return "Some data from C";
},
type: 'TestMultiFunctions'
});
Client Script:
function onLoad() {
var ga = new GlideAjax('TestMultiFunctions');
ga.addParam('sysparm_name','getAll');
ga.addParam('sysparm_param1','some_data');
ga.addParam('sysparm_param2','some_data');
ga.addParam('sysparm_param3','some_data');
ga.addParam('sysparm_param4','some_data');
ga.addParam('sysparm_param5','some_data');
ga.addParam('sysparm_param6','some_data');
ga.getXMLAnswer(ProcessData);
}
function ProcessData(response) {
var data = JSON.parse(response);
alert(data.functionA);
alert(data.functionB);
alert(data.functionC);
}
Please mark my answer helpful and accept as solution if it helped you 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:46 AM
@gunishi I'm glad that It helped you 😀
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 02:52 AM
I have tried using the code above and am getting the following error:
Error MessageonSubmit script error: TypeError: Cannot read properties of null (reading 'functionA'): function () { [native code] }
I emptied the whole of getfunctionA and replaced it with just a 'return false;' line, and still it is not reading it in correctly in my client script.
Any help with this would be appreciated as the issue isn't with the code in getfunctionA, it is in the calling of the function in getAll.
Thank you in advance,
G