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