Multidimensional Array

kanielb1
Mega Expert

How can I create a multidimensional array in a script include and pass it to a client script?  In the client script, I  am trying to populate a choice list with the values from the script include.  My searchResults array is returning all items in one comma separated string.  I would like for it to return an array of searchResults.

            for (var i = 0; i < length; i++) {
                number = myNewObj.Envelope.Body.Response.Rec[i].Number;
                name = myNewObj.Envelope.Body.Response.Rec[i].Name;
                taxId = myNewObj.Envelope.Body.Response.Rec[i].TaxID;
                street = myNewObj.Envelope.Body.Response.Rec[i].StreetAddress;
                city = myNewObj.Envelope.Body.Response.Rec[i].City;
                state = myNewObj.Envelope.Body.Response.Rec[i].State;
                zip = myNewObj.Envelope.Body.Response.Rec[i].Zip;
                phone = myNewObj.Envelope.Body.Response.Rec[i].PhoneNumber;
                
                searchResults.push(number, taxId, name, street, city, state, zip, phone);
            }

6 REPLIES 6

Masha
Kilo Guru

You can use JSON in your script include to create an object of objects (multi-denominational array of sort), stringify it and pass it back to your client script as part of the Ajax. Then convert it back to the JSON object and loop through it to get the values needed to populate your choice list.

Script Include: 

var GetIncidentTestChoiceValues = Class.create();
GetIncidentTestChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getChoiceValues: function(){
		var obj = {"name1":"value1","name2":"value2","name3":"value3"};
		var str=JSON.stringify(obj);
		return str;
	}, 
	type: 'GetIncidentTestChoiceValues'
});

Client Script: 

var ga = new GlideAjax('GetIncidentTestChoiceValues');
	ga.addParam('sysparm_name', 'getChoiceValues');
	ga.getXML(setChoiceValues);
	
	function setChoiceValues(response){
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		var obj = JSON.parse(answer);
		
		for(var c in obj){
			g_form.addOption('u_testchoice', obj[c], c);
		}
	}

 

 

find_real_file.png

kanielb1
Mega Expert

My searchResults array needs to be one choice in the choice list...(number, taxId, name, street, city, state, zip, phone).  Currently, all items in the response are being pushed to the searchResults array.  So if the length of the array is 2, there are 16 items in the searchResults array and would create 16 choices.  Whereas, it should only create 2 choices. 

Then you need an array of objects where each choice object is an item in an array. Here is an updated script: 

SI: 

var GetIncidentTestChoiceValues = Class.create();
GetIncidentTestChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getChoiceValues: function(){
		var searchResults =[];
		//your forloop to get the values
		for(c=0; c<2; c++){
			var obj = {};
			
			obj.number = "number"+c;
			obj.taxId = "taxId"+c;
			obj.name = "name"+c;
			obj.street ="street"+c;
			obj.city = "city"+c;
			obj.state = "state"+c;
			obj.zip = "zip"+c;
			obj.phone = "phone"+c;

			searchResults.push(obj);
		}

		var str=JSON.stringify(searchResults);
		gs.log(str);
		return str;
	}, 
	type: 'GetIncidentTestChoiceValues'
});

 

Client Script: 

function onLoad() {
	var ga = new GlideAjax('GetIncidentTestChoiceValues');
	ga.addParam('sysparm_name', 'getChoiceValues');
	ga.getXML(setChoiceValues);

	function setChoiceValues(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var arr = JSON.parse(answer);
		//alert(answer);
		for (var a=0; a < arr.length; a++){
			var choice = '';
			for(var c in arr[a]){
				//console.log(c+"-"+arr[a][c]);

				choice +=" "+arr[a][c];
			}
			g_form.addOption('u_testchoice', c, choice);
		}
	}

}

 

find_real_file.png

This is working well.  How would I set the value of the choice to the number for each choice?  I need the number for the next web service call. 

Thank you for all of your help with this.