Can I use one GlideAjax to call several functions from one Script Include

gunishi
Tera Guru

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

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

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 👍✔️

 

Thanks,
Anvesh

View solution in original post

10 REPLIES 10

AnveshKumar M
Tera Sage
Tera Sage

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 👍✔️

 

Thanks,
Anvesh

Hi @AnveshKumar M 

 

Thank you so much, this is exactly what I was looking for!

 

G

@gunishi I'm glad that It helped you 😀

Thanks,
Anvesh

Hi @AnveshKumar M 

 

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