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

Brad Bowman
Kilo Patron
Kilo Patron

It may not like the sys_id parm name - try naming this parameter something like sysid or whatever without the second underscore.  Also make sure you only have one Script Include named CatalogClientUtils.

Slava Savitsky
Giga Sage

Is your script include marked as client-callable? Is it in the same scope as the client script?

 

When you say, it fails, what exactly do you mean? How exactly does it fail?

Sandeep Rajput
Tera Patron
Tera Patron

@Jason_DaSilva There is outString variable used in your client script which isn't defined inside the client script. Try the following and see if it works.

 

        var outString = '';
        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';

Jason_DaSilva
Tera Guru

To answer a few of the questions, yes there is only one Script Include with that name, and it is client callable.  It has various functions for client scripts to use (kind of our library).  I have another GlideAjax call on that script include that works without issue (that is called from a different Catalog Client Script on this same Catalog Item) .  I have changed the sysparam to sysparam_pass_id, still the same outcome.  The alert(myAnswer) displays 'null' (that's exactly what appears in the alert box).
I have also added the var outString declaration with out change in output.

One thing that does work is if I hard code the variable values in the Script include:

		var myID = this.getParameter('sysparm_pass_id');
		var fieldList = this.getParameter('sysparm_fields');
		var aTable = this.getParameter('sysparm_table');

		//test
		myID = '1f18bbf31b2c39504a21db1ee54bcbd2';
		fieldList = 'model,serial_number,asset_tag';
		aTable = 'alm_hardware';


So I guess my next step is to trash this and start it over.  I have tried using copy paste to make sure the sys_parms are the same, but something else must be a miss...  😞