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

@Jason_DaSilva Did you try to test the updated script in an incognito browser window? Sometimes caching can lead to a lot of issues.

Renaming the parm to sysparm_pass_id does not remove the possibility that having a second underscore in the parm name is what is causing the failure! 

If you add a log to the beginning of the SI function do you see that?  Alerting on answer is skipping to the end without stepping through the process.  Have you checked your browser console log to see if there are any helpful errors/messages reported?

I missed this post as I was writing out a reply myself.  I don't think the second underscore is the issue.  I have reworked the code (posted in my latest post in the spoiler) and all my sysparms have 2 underscores.  I got past one part (using the wrong query in the Script Include), but still having issues with passing one of the 2 comma separated strings to the Script Include.  
The latter issues is not a deal breaker, but makes the script include less agile for other use.

Jason_DaSilva
Tera Guru

ok, major face palm.  I've moved up the ranks from lieutenant.... looking at the wrong sys_id.  On the Script include, i should be looking up ci_sys_id, as I am passing the sys_id of the CIs to be looked up on the asset table...
The reasons my tests in Xplore worked is because I grabbed a sys_id from the hardware table....  

Just so I don't come back to this and make the same mistake, I changed the sysparm to 

'sysparm_ci_id_list'

I am still having another issue on the passed string of field names.  This is straight up a comma separated list of field names : 
ga.addParam('sysparm_fields_list', 'model,serial_number,asset_tag');
 
I have also pre-set this to a variable and tried passing the variable:
var theFields = 'model,serial_number,asset_tag';
ga.addParam('sysparm_fields_list', theFields);
both of these are also causing a fail.  I can see this by using test values in the Script Include:

 

		var idList = this.getParameter('sysparm_ci_id_list');
		var fieldList = this.getParameter('sysparm_fields_list');
		var aTable = this.getParameter('sysparm_a_table');

		//test
		//idList = '13cd0ab01be5ad10307a2020f54bcb99,bb0b46b41b25ad10307a2020f54bcbcc';
		fieldList = 'model,serial_number,asset_tag';
		//aTable = 'alm_hardware';

 

if I comment out the test value for fieldList, then this fails.  The strings are the same.  To be honest I am new to using GlideAjax, but this seems very straight forward.  the idList also passes a string of sys_ids and that seems to work fine...  So frustrating...
I have cleaned up both pieces of code if looking at the whole thing will be easier to see if I missed something else...

Spoiler
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var ciList = g_form.getValue('select_the_cis'); 	//string
	var theFields = 'model,serial_number,asset_tag';
	var thisTable = 'alm_hardware';
	
	var ga = new GlideAjax('CatalogClientUtils');
	ga.addParam('sysparm_name', 'GetAssetInfo');
	ga.addParam('sysparm_ci_id_list', ciList);			//pass comma separated string of sys_ids
	ga.addParam('sysparm_fields_list', theFields);		//pass comma separated string of field names
	ga.addParam('sysparm_a_table', thisTable);			//pass string of table name

	ga.getXMLAnswer(GetAssetInfoAnswer);
	function GetAssetInfoAnswer(answer){
			var myAnswer = '';
			myAnswer = answer;
			alert(myAnswer);
			g_form.setValue('selected_asset_info', myAnswer);
		}


}​

Script Include (GetAssetInfo function)
	GetAssetInfo: function(){
		var idList = this.getParameter('sysparm_ci_id_list');
		var fieldList = this.getParameter('sysparm_fields_list');
		var aTable = this.getParameter('sysparm_a_table');

		//test
		//idList = '13cd0ab01be5ad10307a2020f54bcb99,bb0b46b41b25ad10307a2020f54bcbcc';
		//fieldList = 'model,serial_number,asset_tag';
		//aTable = 'alm_hardware';

		var fieldArray =[];
		if(Array.isArray(fieldList)){
			fieldArray = fieldArray.concat(fieldList);
		}else{
			fieldList = fieldList.replace(/\s/g, '');  //remove possible white spaces
			fieldArray = fieldList.split(',');
		}
		
		var outString = '';

		var myGR = new GlideRecord(aTable);
		myGR.addEncodedQuery('ci.sys_idIN'+idList);
		myGR.query();
		while(myGR.next()){
			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);
				}
			}
			//comma separated string of data.
			outString = outString + '\n';
		}
		return outString;
	},​

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.