Fail on GlideAjax

Jason_DaSilva
Tera Guru

I am pretty sure I am missing something small here, but I have been pulling my hair out for the past hour on this...

 

in a catalog client script, I am trying to update a text field to use later on in post processing of a ticket.  Mainly I want to take the CIs that are selected on the form and capture the asset model, serial_number and asset_tag fields.  I have done something similar to capture assets assigned to a selected user, and that works fine, so I am not sure what I am missing.

to make things simple I have hard coded values that I have tested on the script include and get the result I want.  It only seems to fail when running it through GlideAjax.  here is the code that is failing:

		var ga = new GlideAjax('CatalogClientUtils');
		ga.addParam('sysparm_name', 'GetAssetInfo');
		ga.addParam('sysparm_sys_id', '1f18bbf31b2c39504a21db1ee54bcbd2'); // thisID);
		ga.addParam('sysparm_fields', 'model,serial_number,asset_tag');  //theFields);
		ga.addParam('sysparm_table', 'alm_hardware');  //thisTable);

		ga.getXMLAnswer(GetAssetInfoAnswer);
		function GetAssetInfoAnswer(answer){
				var myAnswer = '';
				myAnswer = answer;
				alert(myAnswer);
				outString += 'answer';
			}
		outString += '\n';

 

here is the script include function:

	GetAssetInfo: function(){
		var myID = this.getParameter('sysparm_sys_id');
		var fieldList = this.getParameter('sysparm_fields');
		var aTable = this.getParameter('sysparm_table');

		var fieldArray =[];
		if(Array.isArray(fieldList)){
			fieldArray = fieldArray.concat(fieldList);
		}else{
			fieldList = fieldList.replace(/\s/g, '');  //remove possible white spaces
			fieldArray = fieldList.split(',');
		}
		//gs.info(fieldArray + ' : ' + fieldArray.length)
		var myGR = new GlideRecord(aTable);
		myGR.get(myID);

		var outString = '';
		for(i = 0; i < fieldArray.length; i++){
			var thisField = fieldArray[i].toString();
			if(i==0){
				outString += myGR.getElement(thisField).getLabel() + ' : ' + myGR.getDisplayValue(thisField);
			}else{
				outString += ', ' + myGR.getElement(thisField).getLabel() + ' : ' + myGR.getDisplayValue(thisField);
			}
		}
		//gs.info('outString is :');
		//gs.info(outString);
		//comma separated string of data.
		outString = outString + '\n';
		
		return outString;
	},

When I take the script include code to Xplore and paste in the values, it works fine.  Not sure why it fails with the GlideAjax call, even when I hard code values...

1 ACCEPTED SOLUTION

I did a bit of a mock-up and found that the typeof fieldList = object for some reason, so the string method replace is causing the script to stop running.  I couldn't force it to a string in my brief attempts, but since you're sending in a list of field names (hard-coded or from a field/variable value) then you know it's not going to already be an array, so this works to directly convert the parameter value to an array:

var fieldArray = this.getParameter('sysparm_fields_list').split(',');

then you can trim each array member later if necessary.

 

View solution in original post

10 REPLIES 10

ok, that worked.  I guess my test for array was the fail, so dropping that and documenting in the Catalog Client Script that you have to pass a string.

Thanks for sticking around with me for that.