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

Is your script include available to all application scopes?  If not, can you try changing that setting on the script include?

Also, you're definitely still going to need this line inside your callback function.

var objList = JSON.parse(answer);

With that one exception, I've got this working fine from an incident record.  I'd try it there in your system to see if you can at least get that to work.  I just set up an 'onChange' client script on the incident form to run on change of the 'Category' field.  Everything outputs fine to the console.  Only thing I've changed is how the category is provided (using 'g_form.getValue('category') in the script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	try {
		
		console.log("Widget 'Report Issue Form' Category: " + g_form.getValue('category'));
		
		var ga = new GlideAjax("TBHC");
		ga.addParam('sysparm_name', 'getSubCategory');
		ga.addParam('sysparm_categoryVal', g_form.getValue('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 = 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
						});*/
						console.log('Subcategory: ' + obj.value);
					}
				} 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);
	}
}

I tried doing a client script just to test, but I don't see that running on a Service Portal page. Is this different when it's running from a client controller?

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.

Isn't the property 'subcategory' not 'category'?  Like this:

	c.updateSubcategory = function () {
			c.data.action = "updateSubcategory";
			c.server.update().then(function() {
				c.server.action = undefined;
				c.data.parentCategory = $scope.subcategory;
			})
	};