How To Pass An Array from Script Includes to Catalog Client Scripts

jmiskey
Kilo Sage

I am trying to create an array from a Script Includes, and pass it along to a Catalog Client script, and I am having problems.  I was trying to follow along here: https://community.servicenow.com/community?id=community_question&sys_id=9274e477dbb2d70454250b55ca96..., but there is a problem with the accepted solution.  "getXMLWait" is no longer available, so that doesn't work.  I tried the solution underneath it, but had no luck with that either.

First, here is my Script Include.

var TestApps = Class.create();
TestApps.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getRoles: function(){
	
	//Retrieve roles
	var croles = new GlideRecord('u_lawson_company_codes');
		croles.addQuery('u_inactive',false);
		croles.query();
		
		var arrRoles = [];
		while (croles.next()){
			//Fill array
			arrRoles.push(croles.u_company_code.getDisplayValue().toString());
		}
		
},
	
	type: 'TestApps'
});

It is set up to be Client Callable, and is accessible from all Scopes.  By running the code in a Background Scripts window, I was able to confirm that it is working.  I did a gs.print on arrRoles, and it returned what I expected.

Here is my Catalog Client Script, which is running on the loading of the form:

function onLoad() {

	var tlist=[];

	//call script include to get array
	var ga = new GlideAjax("TestApps");
	ga.addParam("sysparm_name","getRoles");
	ga.getXML(returnCodes);

	function returnCodes(response){
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		alert("Answer: " + answer);
		tlist=answer;
	}

I have other code below that which will look through the array, but it is not quite getting there.  When I open the Catalog Item in Srevice Portal, I get the alert pop-up, but it says "Answer: null". 

So I don't seem to be successfully passing the array from my Script Include to my Catalog Client Script, since the answer variable is Null.  I am thinking  that maybe the "var answer = ..." line needs to be different when trying to pass arrays instead of a single value, but I am not sure what that should look like.

Can anyone help?

Thanks

 

1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

Hello. Your script include needs to return a value to the calling client script. Since the return value needs to be a string you can return a comma separated list of values then convert back to an array in the client script. Something like so:

var TestApps = Class.create();
TestApps.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getRoles: function(){
	
	//Retrieve roles
	var croles = new GlideRecord('u_lawson_company_codes');
		croles.addQuery('u_inactive',false);
		croles.query();
		
		var arrRoles = [];
		while (croles.next()){
			//Fill array
			arrRoles.push(croles.u_company_code.getDisplayValue().toString());
		}
return arrRoles.toString();
		
},
	
	type: 'TestApps'
});

 

 

Then your client script would convert back like:

//call script include to get array
	var ga = new GlideAjax("TestApps");
	ga.addParam("sysparm_name","getRoles");
	ga.getXMLAnswer(returnCodes);

	function returnCodes(response){
		var answer = response; 
		alert("Answer: " + answer);
		tlist=answer.split(",");
	}

 

 

Hope this helps.

 

--David

View solution in original post

5 REPLIES 5

Thanks Mike.  Great article, I will bookmark that one!