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

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.

I was able to get everything working. Thank you SO MUCH for you help!!! 

You are very welcome! Happy to help. 🙂

Hi David

Just wanted to let you know that this solution was super helpful!!! It saved the day with what I was trying to achieve!!!

When writing GlideAjax, I recommend separating your this.getParameter() code from your logic, so you can test it in isolation in Scripting tools such as Xplore.

I wrote a guide on how to best write and troubleshoot GlideAjax here.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022