Using GlideAjax with JSON

adargenzio
Tera Expert

Hi all!

 

I am fairly new with scripting so I'm hoping someone can point me intot eh right direction!

 

I am working with a drop down field whose results are dynamically set based off of the value of another field.  

I am successfully pulling the info I need with GlideAjax but I'm running into issues populating the object values into the dropdown field.

 

Currently when I do array.length it is counting characters in the results instead of objects in the array (157 is the result instead of the 67 items there are)

 

 

Code Below:

 

Script Include - 

var lookupFilter = Class.create();
lookupFilter.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
		
//  WORKING - DISPLAYS EQUIPMENT WITH NO 'UNDEFINED' OR [OBJECT OBJECT]
	
	filterEquipment: function() {
		
		var send = [];
		var location = this.getParameter('sysparm_location');

		var gr = new GlideRecord('x_g_usnn_lpod_equipment');
		gr.addQuery('location', location);
		gr.query();
		while(gr.next()){

			var data = gr.equipment_name.toString();
				//arr.equipment += gr.equipment_name.toString();
			//obj.equipment += gr.equipment_name.toString();
			
			send += data;

		}

		return JSON.stringify(send);
			
	},

	type: 'lookupFilter'

});

Catalog Client Script - 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('lookupFilter');
    ga.addParam('sysparm_name', 'filterEquipment');
    ga.addParam('sysparm_location', g_form.getValue('site_location'));

    ga.getXML(callback);
	
    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var arr = JSON.parse(answer);
		
		//console.log("answer: " + arr.length);

		for (var i = 0; i < answer.length; i++){
			
			if (newValue == 'Bettis') {
				
				//g_form.clearValue('equipment_needed');
				g_form.addOption('equipment_needed', "test", "test");
				
// 			 }
			
		}

// if (newValue == 'Bettis') {
// 			//console.log("test 1"); - SUCCESS
//             g_form.clearValue('equipment_needed');
			//g_form.addOption('equipment_needed', "test", "test");
//             g_form.addOption('equipment_needed', arr.equipment, arr.equipment);
// 			 }
//         else if (newValue == 'Knolls') {
// 			//console.log("test 2"); - SUCCESS
				//g_form.addOption('equipment_needed', "test", "test");
//             // g_form.addOption('request_type', choicevalue, choiceLabel);

//         } else {

//            //console.log("test 3"); - SUCCESS
			

       }

    }

}

The comments at the end are some different things I've tried....

 

Any help is appreciated, and thank you in advance!!

3 REPLIES 3

ahefaz1
Mega Sage

@adargenzio ,

 

For adding values to the array can you try and use the push method

send.push(data).

then on the client side you can do a split(',') which will convert the stringified array to an array again. Once you split the array back again, the length should be equal to the number of items you pushed on the server side.

 

var arr = answer.split(",");

 

Please mark helpful if this helped.

 

Thanks,

Sandeep Rajput
Tera Patron
Tera Patron

@adargenzio Please update your Script Include script as follows.

Script Include:

var lookupFilter = Class.create();
lookupFilter.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
		
//  WORKING - DISPLAYS EQUIPMENT WITH NO 'UNDEFINED' OR [OBJECT OBJECT]
	
	filterEquipment: function() {
		
		var send = [];
		var location = this.getParameter('sysparm_location');

		var gr = new GlideRecord('x_g_usnn_lpod_equipment');
		gr.addQuery('location', location);
		gr.query();
                var locationArray = [];
		while(gr.next()){

			var data = gr.equipment_name.toString();
				//arr.equipment += gr.equipment_name.toString();
			//obj.equipment += gr.equipment_name.toString();
			locationArray.push(gr.equipment_name.toString());
			//send += data;

		}

		return JSON.stringify(locationArray);			
	},

	type: 'lookupFilter'

});

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('lookupFilter');
    ga.addParam('sysparm_name', 'filterEquipment');
    ga.addParam('sysparm_location', g_form.getValue('site_location'));

    ga.getXML(callback);
	
    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var arr = JSON.parse(answer);
		
		//console.log("answer: " + arr.length);

		for (var i = 0; i < arr.length; i++){
			
			if (newValue == 'Bettis') {
				
				//g_form.clearValue('equipment_needed');
				g_form.addOption('equipment_needed', "test", "test");
				
// 			 }
			
		}

// if (newValue == 'Bettis') {
// 			//console.log("test 1"); - SUCCESS
//             g_form.clearValue('equipment_needed');
			//g_form.addOption('equipment_needed', "test", "test");
//             g_form.addOption('equipment_needed', arr.equipment, arr.equipment);
// 			 }
//         else if (newValue == 'Knolls') {
// 			//console.log("test 2"); - SUCCESS
				//g_form.addOption('equipment_needed', "test", "test");
//             // g_form.addOption('request_type', choicevalue, choiceLabel);

//         } else {

//            //console.log("test 3"); - SUCCESS
			

       }

    }

}

Hope this helps.

Tai Vu
Kilo Patron
Kilo Patron

Hi @adargenzio 

It's because of this line below in your client script. You're using the answer instead of arr variable.

for (var i = 0; i < answer.length; i++)

 

So just changes to

for (var i = 0; i < arr.length; i++)

 

Cheers,

Tai Vu