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

Hi @gunishi 

Are you using this on onSubmit client script? Then modify the client script to the one like below.

 

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.getXMLWait();
   var response = ga.getAnswer();
   var data = JSON.parse(response);
   alert(data.functionA);
   alert(data.functionB);
   alert(data.functionC);
}

 

if the above didn't work check the contents of response variable using alert(response); and access the data accordingly.

 

Thanks,
Anvesh

Hi @AnveshKumar M 

 

Thank you for taking the time to help me, I really appreciate it. 

 

When I am checking the contents of response it is 'null', is there anyway for me to troubleshoot this simply?

 

No worries if not and I will press on and keep playing with the code until it works.

 

Kind regards,

G

Hi @AnveshKumar M 

 

I managed to solve the issue. 

 

The problem was that after var data has been declared, when qualifying the values the user needs to call it from the same script include:

data.functionA = this.getfunctionA;

 

Thank you for all your help, it gave me a great starting point!

@gunishi Ohh my Bad! I missed that this part. Some times we miss the basic things, especially when writing in hurry. 😀

 

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 = this.getFunctionA(param1, param2);
		data.functionB = this.getFunctionA(param3, param4);
		data.functionC = this.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'
});

 

 

Thanks,
Anvesh

Hi @AnveshKumar M 

 

Don't worry at all! It was a good exercise for me for learning purposes, and your code gave me a brilliant starting point. 

 

G