Unhandled exception in GlideAjax

Lisa Silvaroli
Tera Guru

I have a catalog client script that keeps giving "There is a JavaScript error in your browser console" 

The console just says "Unhandled exception in GlideAjax."

This is the code I believe it is having the issue with:

var ga = new GlideAjax('StaffInfoAjax3');
			ga.addParam('sysparm_name', 'getStaffInfo');
			ga.addParam('sysparm_staff', staffId);
			ga.addParam('sysparm_access_action', pass_action);
			ga.getXMLAnswer(response);
			ga.getXMLAnswer(function(response){
			response = JSON.parse(response);

What do I need to change to make it work? 

1 ACCEPTED SOLUTION

This is the best way to learn! 🙂

 

Ok so my preferred way to return multiple values is to place them into an object then stringify (convert the object to a string) and return to the client script. The client script can then convert back to an object and you can do with the values as you please. For example taking your script above (getUserName) lets say your wanted to return multiple values from the user record for use in the client script it would look like this: p.s ive changed the name of the function to getUserDetails

var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	
	getUserDetails: function(){
                var obj = {};		
		var userRecord = new GlideRecord("sys_user");
		if(userRecord.get(this.getParameter('sysparm_userID'))){
                     
                  
                  obj.first_name = userRecord.getValue('first_name');
                  obj.last_name = userRecord.getValue('last_name');
                  obj.email = userRecord.getValue('email');
                  //Reference Field will return sys_id. Use getDisplayValue() to return display value
                  obj.manager = userRecord.getValue('manager');

                }
		
                 return JSON.stringify(obj);         
	},


    type: 'GetUserInfo'
});

 

Then to consume the multiple returned values you can parse the returned object string:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 
	if (isLoading || newValue === '' || newValue == oldValue) {
		return;
	}
	
	var getUserIn = new GlideAjax('GetUserInfo');
	getUserIn.addParam('sysparm_name','getUserDetails');
	getUserIn.addParam('sysparm_userID', g_form.getValue('sn_userid'));
	getUserIn.getXML(populateUserDetails);
	
	
	function populateUserDetails(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var obj = JSON.parse(answer);

                g_form.clearValue('user_firstname');
		g_form.setValue('user_firstname', obj.first_name);

                g_form.clearValue('user_lastname');
		g_form.setValue('user_lastname', obj.last_name);

                g_form.clearValue('user_email');
		g_form.setValue('user_email', obj.email);

                //Assuming this is a reference field to sys_user table which will consume returned manager sys_id
                g_form.clearValue('user_manager');
		g_form.setValue('user_manager', obj.manager);
	}
	
}

 

Hopefully this helps a bit.

View solution in original post

21 REPLIES 21

So try the below modification to your script include. Couple things to point out: getValue casts the return value as a String so no need to do that a second time also the ajax call returned value has to be a string so instead of JSON.encode() that is replaced with JSON.stringify():

 

var StaffInfoAjax3 = Class.create();
StaffInfoAjax3.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getStaffInfo: function() {
		var usrId = this.getParameter('sysparm_staff');
		var usrRec = new GlideRecord('sys_user');
		if (usrRec.get(usrId)){
		

		    var obj = {};

		    obj.u_district = usrRec.getValue('u_district');
		    obj.u_districtDisp = usrRec.u_district.getDisplayValue();
		    obj.u_building = usrRec.getValue('u_building');
		    obj.u_buildingDisp = usrRec.u_building.getDisplayValue();
		    obj.u_room_number = usrRec.getValue('u_room__');
		    obj.u_phone_number = usrRec.getValue('phone');
		    obj.firstname = usrRec.getValue('first_name');
		    obj.lastname = usrRec.getValue('last_name');
		    obj.title = usrRec.getValue('title');
		    obj.email = usrRec.getValue('email'); 

		    return JSON.stringify(obj);

	}
},

Still getting Unhandled exception in GlideAjax. 

Here is both the client script and script include in their entirety:

Client Script 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}
	
	if (newValue != oldValue) {
		var staffId = newValue;
		var pass_action = g_form.getValue('variables.access_action').toString();
		//refreshApps(oldValue, staffId);
		if (newValue != '') {
			
			// Clear all options before setting them appropriately
			g_form.setValue('ad_email', 'false');
			g_form.setValue('file_folder_access', 'false');
			g_form.setValue('vpn', 'false');
			g_form.setValue('raw', 'false');
			g_form.setValue('servicenow', 'false');
			g_form.setValue('edm', 'false');
			g_form.setValue('idata', 'false');
			//g_form.setValue('fm', 'false');
			//g_form.setValue('munis', 'false');
			g_form.setValue('nvision', 'false');
			g_form.setValue('cleartrack', 'false');
			//g_form.setValue('edoctrina', 'false');
			//g_form.setValue('eschoolplus', 'false');
			g_form.setValue('rtiedge', 'false');
			g_form.setValue('schooltool', 'false');
			g_form.setValue('devices', 'false');
			//g_form.setValue('phone_desktop', 'false');
			//g_form.setValue('phone_cell', 'false');
			g_form.setValue('phone_district', 'false');
			g_form.setValue('badge', 'false');
			g_form.setValue('eschoolview', 'false');
			g_form.setValue('key_fob', 'false');
			g_form.setValue('mylearningplan', 'false');
			//g_form.setValue('nutrikids', 'false');
			g_form.setValue('oasys', 'false');
			g_form.setValue('employee_id', '');
			
			
			var ga = new GlideAjax('StaffInfoAjax3');
			ga.addParam('sysparm_name', 'getStaffInfo');
			ga.addParam('sysparm_staff', staffId);
			ga.addParam('sysparm_access_action', pass_action);
			
			ga.getXMLAnswer(function(response){
				console.log(response + "test");
				//var resp = JSON.parse(response);
// 				var answer = ga.getXMLAnswer();
// 					alert('Test ' +answer);
							

				// 3-13-18 crossfuze changed line abvove from response=response.evalJSON() to response = JSON.parse(response) to work in the service portal
				
				
// 				if (response.u_district) {
// 					g_form.setValue('u_district', resp.u_district, resp.u_districtDisp);
// 				}
				
// 				if (response.u_building) {
// 					g_form.setValue('primary_location', resp.u_building, resp.u_buildingDisp);
// 				}
				
// 				if (response.u_room_number) {
// 					g_form.setValue('room_number', resp.u_room_number);
// 				}
				
				if (resp.firstname) {
					g_form.setValue('user_firstname', resp.firstname);
					
				}
				
// 				if (response.lastname) {
// 					g_form.setValue('user_lastname', resp.lastname);
// 				}
				
// 				if (response.title) {
// 					g_form.setValue('job_title', resp.title);
// 				}
				
// 				if (response.u_phone_number) {
// 					g_form.setValue('phone', resp.u_phone_number);
// 				}
				
// 				if (response.email) {
// 					g_form.setValue('email', resp.email);
// 				}
				
							
			});
			
		
			if ((g_form.getValue('access_action') == 'userdelete') || (g_form.getValue('access_action') == 'userdisable')) {
				var ga2 = new GlideAjax('StaffInfoAjax3');
				ga2.addParam('sysparm_name', 'getStaffAccess');
				ga2.addParam('sysparm_staff', staffId);
				ga2.addParam('sysparm_access_action', pass_action);
				ga2.getXMLAnswer(function(response){
					response = JSON.parse(response);
					//response = response.evalJSON();
					
										
					if (response.Email) {
						g_form.setValue('ad_email', 'true');
					}
					
					if (response.FileFolderAccess) {
						g_form.setValue('file_folder_access', 'true');
					}
					
					if (response.Raw) {
						g_form.setValue('raw', 'true');
					}
					
					if (response.ServiceNow) {
						g_form.setValue('servicenow', 'true');
					}
					
					//if (response.FM) {
						//g_form.setValue('fm', 'true');
					//}
					
					//if (response.Munis) {
						//g_form.setValue('munis', 'true');
					//}
					
					if (response.nVision) {
						g_form.setValue('nvision', 'true');
					}
					
					if (response.Devices) {
						g_form.setValue('devices', 'true');
					}
					
					//if (response.DesktopPhone) {
						//g_form.setValue('phone_desktop', 'true');
					//}
					
					//if (response.CellPhone) {
						//g_form.setValue('phone_cell', 'true');
					//}
					
					if (response.Edm) {
						g_form.setValue('edm', 'true');
					}
					
					if (response.iData) {
						g_form.setValue('idata', 'true');
					}
					
					if (response.Cleartrack) {
						g_form.setValue('cleartrack', 'true');
					}
					
					//if (response.eDoctrina) {
						//g_form.setValue('edoctrina', 'true');
					//}
					
					//if (response.eSchoolplus) {
						//g_form.setValue('eschoolplus', 'true');
					//}
					
					//if (response.Powerschool) {
						//g_form.setValue('powerschool', 'true');
					//}
					
					if (response.RtiEdge) {
						g_form.setValue('rtiedge', 'true');
					}
					
					if (response.Schooltool) {
						g_form.setValue('schooltool', 'true');
					}
					
					//if (response.AutoNotification) {
						//g_form.setValue('autonotification', 'true');
					//}
					
					if (response.eSchoolView) {
						g_form.setValue('eschoolview', 'true');
					}
					
					if (response.MyLearningPlan) {
						g_form.setValue('mylearningplan', 'true');
					}
					
					//if (response.Nutrikids) {
						//g_form.setValue('nutrikids', 'true');
					//}
					
					if (response.Oasys) {
						g_form.setValue('oasys', 'true');
					}
					
					if (response.VPN) {
						g_form.setValue('vpn', 'true');
					}
					
					if (response.KeyFob) {
						g_form.setValue('key_fob', 'true');
					}
					
					if (response.DistrictPhone) {
						g_form.setValue('phone_district', 'true');
					}
					
					if (response.Badge) {
						g_form.setValue('badge', 'true');
					}
					
					
				});
			}
			
			
		}
		else {
			
			g_form.clearValue('u_district');
			g_form.clearValue('primary_location');
			g_form.clearValue('room_number');
			g_form.clearValue('phone');
			g_form.clearValue('user_firstname');
			g_form.clearValue('user_lastname');
			g_form.clearValue('job_title');
			g_form.clearValue('email');
			
		}
		
		processUpdateRequestedForInfo(staffId);
		
	}
}





function processUpdateRequestedForInfo(requestedForId) {
	
	var ga = new GlideAjax('StaffInfoAjax3');
	ga.addParam('sysparm_name', 'getRequestedForInfoStr');
	ga.addParam('sysparm_user', requestedForId);
	ga.addParam('sysparm_access_action', g_form.getValue('access_action'));
	ga.getXML(updateRequestedForInfo);
	
	function updateRequestedForInfo(serverResponse) {
		var result = serverResponse.responseXML.getElementsByTagName('result');
		
		if (result != null && result.length > 0) {
			
			var userInfo = result[0].getAttribute('requestedForInfo');
			g_form.setReadOnly('user_application_access', true);
			g_form.setDisplay('user_application_access', true);
			//this.document.getElementById(g_form.getControl('user_application_access').id + '_ifr').style.height = "65px";
			
			g_form.setValue('user_application_access', userInfo);
			
			if (!userInfo) {
				g_form.addInfoMessage('There are no devices and no lines assigned to user');
			}
			//this.document.getElementById(g_form.getControl('user_application_access').id + '_ifr').style.height = "65px";
			g_form.setValue('user_application_access', userInfo);
			//g_form.setReadOnly('user_application_access',true);
			
		} else {
			g_form.setDisplay('user_application_access', false);
		}
	}
}

Script include:

var StaffInfoAjax3 = Class.create();
StaffInfoAjax3.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
// 	getStaffInfo: function() {
// 		var usrId = this.getParameter('sysparm_staff');
// 		var usrRec = new GlideRecord('sys_user');
// 		usrRec.get(usrId);
// 		if (usrRec) {
// 			//var district = String(usrRec.getValue('u_district'));
// // 			var building = String(usrRec.getValue('u_building'));
// // 			var roomNumber = String(usrRec.getValue('u_room__'));
// // 			var phoneNumber = String(usrRec.getValue('phone'));
//  			var firstname = String(usrRec.getValue('first_name'));
// 			var lastname = String(usrRec.getValue('last_name'));
// // 			var title = String(usrRec.getValue('title'));
// // 			var districtDisp = usrRec.u_district.getDisplayValue();
// // 			var buildingDisp = usrRec.u_building.getDisplayValue();
// // 			var email = String(usrRec.getValue('email'));
// 			return new JSON().encode({u_district: district, u_districtDisp: districtDisp, u_building: building, u_buildingDisp: buildingDisp, u_room_number: roomNumber, u_phone_number: phoneNumber, firstname: firstname, lastname: lastname, title: title, email: email });
// 			}
// 		},
	
		getStaffInfo: function() {
		var usrId = this.getParameter('sysparm_staff');
		var usrRec = new GlideRecord('sys_user');
		if (usrRec.get(usrId)){
		

		    var obj = {};

		    obj.u_district = usrRec.getValue('u_district');
		    obj.u_districtDisp = usrRec.u_district.getDisplayValue();
		    obj.u_building = usrRec.getValue('u_building');
		    obj.u_buildingDisp = usrRec.u_building.getDisplayValue();
		    obj.u_room_number = usrRec.getValue('u_room__');
		    obj.u_phone_number = usrRec.getValue('phone');
		    obj.firstname = usrRec.getValue('first_name');
		    obj.lastname = usrRec.getValue('last_name');
		    obj.title = usrRec.getValue('title');
		    obj.email = usrRec.getValue('email'); 

		    return JSON.stringify(obj);

	}
},
		
		
		getStaffAccess: function() {
			var usrId = this.getParameter('sysparm_staff');
			var accessAction = this.getParameter('sysparm_access_action');
			var usrRec = new GlideRecord('u_user_application_access');
			usrRec.addQuery('u_sn_userid', usrId);
			usrRec.addQuery('u_access_removed_yn', 'false');
			if ((accessAction == 'userdisable') || (accessAction == 'userchange')) {
				usrRec.addQuery('u_app_action', 'Add');
			}
			else {
				var orCond = usrRec.addQuery('u_app_action', 'Add');
				orCond.addOrCondition('u_app_action', 'Disable');
			}
			usrRec.query();
			var Email = false;
			var FileFolderAccess = false;
			var Raw = false;
			var ServiceNow = false;
			var FM = false;
			var Munis = false;
			var nVision = false;
			var Devices = false;
			var DesktopPhone = false;
			var CellPhone = false;
			var Edm = false;
			var iData = false;
			var Cleartrack = false;
			var eDoctrina = false;
			var eSchoolplus = false;
			var Powerschool = false;
			var RtiEdge = false;
			var Schooltool = false;
			var AutoNotification = false;
			var eSchoolView = false;
			var MyLearningPlan = false;
			var Nutrikids = false;
			var Oasys = false;
			var VPN = false;
			var Badge = false;
			var KeyFob = false;
			var DistrictPhone = false;
			
			var appAccess = [];
			
			//gs.log('KAREN PASS IN THE SCRIPT INCLUDE ' + usrId);
			
			
			while (usrRec.next()) {
				//gs.log('KAREN PASS IN THE WHILE');
				
				
				appAccess.push(usrRec.u_app_access.getDisplayValue());
				
				//gs.log('KAREN ' +usrRec.u_app_access.getDisplayValue() ,"APP ACCESS debug");
				
				if (usrRec.u_app_access.getDisplayValue() == 'Email/Network Logon') {
					Email = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'File/Folder Access') {
					FileFolderAccess = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'RicAnywhere') {
					Raw = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'ServiceNow') {
					ServiceNow = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'FM') {
					FM = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Munis') {
					Munis = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'nVision') {
					nVision = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Devices') {
					Devices = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Desktop Phone') {
					DesktopPhone = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Cell Phone') {
					CellPhone = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'EDM') {
					Edm = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'iData') {
					iData = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Cleartrack') {
					Cleartrack = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'eDoctrina') {
					eDoctrina = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'eSchoolPlus') {
					eSchoolplus = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Powerschool') {
					Powerschool = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'RTI Edge') {
					RtiEdge = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Schooltool') {
					Schooltool = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Auto-Notification') {
					AutoNotification = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'eSchoolView') {
					eSchoolView = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'My Learning Plan') {
					MyLearningPlan = true;
				}
				if (usrRec.u_app_access.getDisplayValue() == 'Nutrikids') {
					Nutrikids = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'OASYS') {
					Oasys = true;
				}
				
				if ((usrRec.u_app_access.getDisplayValue() == 'Remote Desktop Access') || (usrRec.u_app_access.getDisplayValue() == 'VPN')) {
					VPN = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Key Fob') {
					KeyFob = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'District Phone') {
					DistrictPhone = true;
				}
				
				if (usrRec.u_app_access.getDisplayValue() == 'Badge') {
					Badge = true;
				}
				
			}
			//gs.log('KAREN BEFORE JSON ' + Email + ' ' + ServiceNow);
			return new JSON().encode({Email: Email, ServiceNow: ServiceNow, FileFolderAccess: FileFolderAccess, Raw: Raw, FM: FM, Munis: Munis, nVision: nVision, Devices: Devices, DesktopPhone: DesktopPhone, CellPhone: CellPhone, Edm: Edm, iData: iData, Cleartrack: Cleartrack, eDoctrina: eDoctrina, eSchoolplus: eSchoolplus, Powerschool: Powerschool, RtiEdge: RtiEdge, Schooltool: Schooltool, AutoNotification: AutoNotification, eSchoolView: eSchoolView, MyLearningPlan: MyLearningPlan, Nutrikids: Nutrikids, Oasys: Oasys, VPN: VPN, KeyFob: KeyFob, DistrictPhone: DistrictPhone, Badge:Badge,    u_ap_access: appAccess} );
			},
			
			getRequestedForInfoStr: function() {
				var userId = this.getParameter('sysparm_user');
				var accessAction2 = this.getParameter('sysparm_access_action');
				var gr = new GlideRecord('u_user_application_access');
				gr.addQuery('u_sn_userid', userId);
				gr.addQuery('u_access_removed_yn', 'false');
				if (accessAction2 == 'userdisable') {
					gr.addQuery('u_app_action', 'Add');
				}
				else {
					var orCond2 = gr.addQuery('u_app_action', 'Add');
					orCond2.addOrCondition('u_app_action', 'Disable');
				}
				
				gr.query();
				
				var resultHeader = '<table width="100%" style="border:none"><tbody style="text-align: left"><tr>' +
				'<th style="border:none">Applications Access</th>' +
				'<th style="border:none">Requested Item</th></tr>';
				var resultBody = '';
				var resultStr = '';
				
				while (gr.next()) {
					
					
					resultBody += '<tr><td style="border:none">' + (gs.nil(gr.u_app_access.getDisplayValue()) ? '' : gr.u_app_access.getDisplayValue()) + '</td>' +
					'<td style="border:none"><a onClick="javascript:openTicket(' +gr.u_requested_item.number +')">' + (gs.nil(gr.u_requested_item.number) ? '' : gr.u_requested_item.number) + '</a></td></tr>';
					//'<td style="border:none">a' + (gs.nil(gr.u_requested_item.number) ? '' : gr.u_requested_item.number) + '</td></tr>';
					

					
					
					
					resultStr = resultHeader + resultBody + '</tbody></table>';
					
					
				}
				
				var result = this.newItem('result');
				result.setAttribute('requestedForInfo', resultStr);
				gs.log(result, "app access debug");
				gs.log(gr.u_app_access.getDisplayValue());
			},
			
			
		});

Each Ajax call needs to use JSON.stringify() to return the values to the client script instead of JSON. encode(). So I'd change all of those. Also does the error point to the same Ajax call or a different one?

Ok so I started back from square one instead of trying to tweak the existing code that was not working. 

I've finally gotten it to at least populate 1 field/variable when I fill in the user ID instead of just giving me errors. 

Do you have an advice on how to properly add multiple values now? Here is my new working code:

Script include:

var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	
	getUserName: function(){		
		var userRecord = new GlideRecord("sys_user");
		userRecord.get(this.getParameter('sysparm_userID'));
		return userRecord.first_name + '';         
	},
    type: 'GetUserInfo'
});

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  // Modified the if to return if the newValue == oldValue to avoid
	// unecessary trips to the server
	if (isLoading || newValue === '' || newValue == oldValue) {
		return;
	}
	
	var getUserIn = new GlideAjax('GetUserInfo');
	getUserIn.addParam('sysparm_name','getUserName');
	getUserIn.addParam('sysparm_userID', g_form.getValue('sn_userid'));
	getUserIn.getXML(populateFirstName);
	
	
	function populateFirstName(response){
		var userInfofromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
		g_form.clearValue('user_firstname');
		g_form.setValue('user_firstname',userInfofromScriptInclude);
	}
	
}

I really appreciate your help on this! This is my first time working with the glideajx and I am not really great with code to begin with! (learning on the fly) 

I think your script function name(getUserName) is colliding with OOB script includes

Just appended _1 to the funtion name to make it unique

Can you try with the below scripts and see if it works

 

//Script Include
var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	
	getUserName_1: function(){
		var userRecord = new GlideRecord("sys_user");
		userRecord.get(this.getParameter('sysparm_userID'));
		return userRecord.first_name + '';
	},
	type: 'GetUserInfo'
});


//Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	// Modified the if to return if the newValue == oldValue to avoid
	// unecessary trips to the server
	if (isLoading || newValue === '' || newValue == oldValue) {
		return;
	}
	
	var getUserIn = new GlideAjax('GetUserInfo');
	getUserIn.addParam('sysparm_name','getUserName_1');
	getUserIn.addParam('sysparm_userID', g_form.getValue('sn_userid'));
	getUserIn.getXML(populateFirstName);
	
	
	function populateFirstName(response){
		var userInfofromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
		g_form.clearValue('user_firstname');
		g_form.setValue('user_firstname',userInfofromScriptInclude);
	}
	
}