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

Michael Fry1
Kilo Patron

I think - sc.addQuery('name', 'incident'); - should be - sc.addQuery('table', 'incident');

and maybe - sc.addQuery('parent', categoryVal); - should be - sc.addQuery('dependent_value', categoryVal);

I would also add a check for active choices only

So I checked the "sys_choice" table and I did find that name is the correct field for the table. I did update to this:

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();

 

Still getting the same error 😞

Mark Stanger
Giga Sage

Looks like you need to initialize your 'list' array right before your while loop in your script include.

var list = [];

Added that before you posted, but thanks for the suggestion. Still having the same error. I am stumped.