GlideAjax - Unhandled Exception

ShaneFr
Mega Expert

Dear SN Community -

I am having a major error with GlideAjax in a Widget that is in a Scoped App that is on the Service Portal. I have already reviewed these sties;

  • https://github.com/chucktomasi/technow-glideajax
  • https://snprotips.com/blog/2016/2/6/gliderecord-client-side-vs-server-side
  • https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f96196e

So you have it for the Script Include settings:

  • Client callable is checked.
  • Accessible from: All Application Scopes
  • Protection Policy

The Script Include:

var TBHC_BK = Class.create();
TBHC_BK.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	getSubCategory: function() {
		var categoryVal = this.getParameter('sysparm_categoryVal');
		var sc = new GlideRecord('sys_choice');
		sc.addQuery('name', 'incident');
		sc.addQuery('element', 'subcategory');
		sc.addQuery('parent', categoryVal);
		sc.query();
		
		while (sc.next()) {
			var eObj = {};
			eObj.value = sc.getValue('value');
			eObj.label = sc.getValue('label');
			list.push(eObj);
		}
		return JSON.stringify(list);
	}
	
});

For the Client Script Call on the Widget:

function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout, $window) {
	
	var c = this;
	
	$scope.updateSubcategory = function () {
		$scope.data.subcategory = [];
		priCategory = $scope.category;
		try {
			var ga = new GlideAjax('TBHC_BK');
			ga.addParam('sysparm_name', 'getSubCategory');
			ga.addParam('sysparm_categoryVal', priCategory);
			ga.getXMLAnswer(subcategoryCallback);
		
			function subcategoryCallback(answer) {
				var objList = JSON.parse(answer);
				for (var i = 0; i < objList.length; i++) {
					var obj = objList[i];
					$scope.data.subcategory.push({
						value: obj.value,
						label: obj.label
					});
				}
			}
		} catch (err) {
			console.log("ERROR - " + err);
		}
		
	};

	
}

Any help would be grateful.

Thanks,

Shane

 

 

1 ACCEPTED SOLUTION

ShaneFr
Mega Expert

Dear SN Community - 

So after many hours (And I think I was a but upset yesterday) I re-looked and I did this: https://www.youtube.com/watch?v=sr_gqqxllRM

Client Controller:

function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout, $window) {
	
	var c = this;
	
	c.updateSubcategory = function () {
			c.data.action = "updateSubcategory";
			c.server.update().then(function() {
				c.server.action = undefined;
				c.data.parentCategory = $scope.category;
			})
	};

}


			

Server Side Script on Widget:

if (input) {
		
		// Update Subcategory
		if (input.action == "updateSubcategory") {
			data.subcategory = [];
			var grSubCategory = new GlideRecord('sys_choice');
			grSubCategory.addQuery('name', 'incident');
			grSubCategory.addQuery('element', 'subcategory');
			grSubCategory.addQuery('dependent_value', input.parentCategory);
			grSubCategory.addQuery('inactive', 'false');
			grSubCategory.query();
			while (grSubCategory.next()) {
				data.subcategory.push({
					value: grSubCategory.getValue('value'),
					label: grSubCategory.getValue('label')
				});		
			}
		}
		
	}

This subsequently worked.

View solution in original post

14 REPLIES 14

Try replacing this line...

var objList = JSON.parse(answer);

with this...

var objList = answer.evalJSON(true);

If that still doesn't work, it's probably time to add in some logging in both scripts to see if you can find exactly where things are breaking.

In your client script you also need to initialize your variable on this line.

var priCategory = $scope.category;

So I have been putting gs.info/gs.warn in my client script. "TBHC_BK" and I never see it even get executed anywhere in the script no in any logs.

So here is the console from the client script.

Widget 'Report Issue Form' Category: software

Widget 'Report Issue Form' GA Object: [object

Widget 'Report Issue Form' Answer: null

Widget 'Report Issue Form': Answer Empty

Widget 'Report Issue Form' Category: inquiry

Widget 'Report Issue Form' GA Object: [object Object]

Widget 'Report Issue Form' Answer: null

Widget 'Report Issue Form': Answer Empty

 

Can you please post your latest scripts? I got this to work just fine from a standard incident form so I know I've got the ajax working. If you post all of the details of your widget so I can set it up in my system I'd be happy to help debug.

Sever Include Script:

var TBHC = Class.create();
TBHC.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	getSubCategory: function() {
		gs.info('Are we being fired?', 'TBHC_BK');
		gs.info('Fire TBHC:getSubCategory', 'TBHC_BK');
		try {
			var categoryVal = this.getParameter('sysparm_categoryVal');

			var sc = new GlideRecord('sys_choice');
			sc.addQuery('name', 'incident');
			sc.addQuery('element', 'subcategory');
			sc.addQuery('dependent_value', categoryVal);
			sc.addQuery('inactive', 'false');
			sc.query();

			var list = [];

			while (sc.next()) {
				var eObj = {
					value: sc.getValue("value"),
					label: sc.getValue("label")
				};
				list.push(eObj);
			}
			return JSON.stringify(list);
		} catch (e) {
			gs.info('TBHC:' + e, 'TBHC_BK');
		}
	},
	
	type: 'TBHC'
	
});

 

Widget Client Script:

function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout, $window) {
	
	var c = this;
	$scope.data.subcategory = [];
	
	$scope.updateSubcategory = function () {
		
		try {
			
			console.log("Widget 'Report Issue Form' Category: " + $scope.category);
			
			var ga = new GlideAjax("TBHC");
			ga.addParam('sysparm_name', 'getSubCategory');
			ga.addParam('sysparm_categoryVal', $scope.category);
			ga.getXMLAnswer(subcategoryCallback);
			
			console.log("Widget 'Report Issue Form' GA Object: " + ga);
			
			
			function subcategoryCallback(answer) {
				try {
					console.log("Widget 'Report Issue Form' Answer: " + answer);
					if (answer) {
						var objList = answer;
						for (var i = 0; i < objList.length; i++) {
							var obj = objList[i];
							$scope.data.subcategory.push({
								value: obj.value,
								label: obj.label
							});
						}
					} else {
						console.log("Widget 'Report Issue Form': Answer Empty");
					}
				} catch (e) {
					console.log("Widget 'Report Issue Form' - subcategoryCallback: " + e);
				}
			}
			
		} catch (e) {
			console.log("Widget 'Report Issue Form': " + e);
		}
		
	};

}