GlideAjax not working in client script

Ahmad6
Giga Expert

Hi guys,

 

I've tried to follow the guide at https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f96196e to create a client script which uses script include but the script is not accessing the script include (or i'm not reading the data correctly). I have used this process in other Client scripts in slightly different manners and they work fine so i'm not sure what's happening, if I can get another set of eyes that would be greatly appreciated.

The table inherits the incident table, and uses a matrix of category/subcategory to determine the priority/SLA duration.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

	var ga = new GlideAjax('AL_SCRIPT');
	ga.addParam('sysparm_name','getPriority');
	ga.addParam('sysparm_category', g_form.getValue('category'));
	ga.addParam('sysparm_subcategory', g_form.getValue('subcategory'));
	ga.getXML(parseAjax);
	
}

function parseAjax(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.addInfoMessage('priority: ' + answer);
	if(answer){		
		g_form.setValue('priority',answer);
	}
	
}

script include below:

var AL_SCRIPT = Class.create();
AL_SCRIPT.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getPriority:  function () {
	gs.addInfoMessage("finding priority");
	var category = this.getParameter('sysparm_category');
	var subcategory = this.getParameter('sysparm_subcategory');
	
	var gr = new GlideRecord('u_ncr_matrix');
	gr.addQuery('u_category', category);
	gr.addQuery('u_subcategory', category);
	gr.query();
	if(gr.next()){
		return gr.u_priority;
	} else return false;
},


type: 'AL_SCRIPT'
});
1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Might be a silly question but have you checked that "Client callable" is ticked on your script include?

find_real_file.png

View solution in original post

28 REPLIES 28

Hi Brent,

Am I correct in assuming that the only difference is that you have dropped the following line?

var answer = response.responseXML.documentElement.getAttribute("answer");

Edit: the answer printed is [object Object]

I have used "ga.getXMLAnswer" instead of "getXML". This will extract the "answer" without the need for "var answer = response.responseXML.documentElement.getAttribute("answer");"

Drop the whole code snippet into your client script and let me know how you get along.

Hi Brent,

The answer is still null with ga.getXMLAnswer. I've changed the script include to send '3' rather than calculate anything to eliminate all of the calculations being made in the script include. Answer is still null, so I'm confident that the script itself is never being reached.

In the script include are you setting the "priority" variable using getValue? Should look like the following:

priority = gr.getValue('u_priority'); //should use getValue to ensure it is returned as a string

 

Ahmad6
Giga Expert

I have another GlideAjax which works fine, and I can't see the difference between the two structurally:

 

function onLoad() {

 
	var ga = new GlideAjax('AL_checkUserGroup');
	ga.addParam('sysparm_name','isMemberOf');
	ga.addParam('sysparm_group', 'NCR Admin');
	ga.getXML(parseAjax);
	
}

function parseAjax(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if(answer){
		g_form.addOption('u_action', 'Cancel', 'Cancel');
		  if(g_form.getValue('state') == 3) g_form.addOption('u_action', 'Approve', 'Approve');
		else if(g_form.getValue('state') == 7) g_form.addOption('u_action', 'Re-Open', 'Re-Open');
	} 
	
}

 

var AL_checkUserGroup = Class.create();
AL_checkUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor,  {
	
	isMemberOf: function(){
		var userID = gs.getUserID();
		var groupName = this.getParameter('sysparm_group');
		//gs.addInfoMessage("group name: " + groupName + ", userid " + userID)
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', userID);
		gr.addQuery('group.name', groupName);
		gr.query();
		if(gr.next()) {
			//gs.addInfoMessage("is a member")
			return true;
		}
		else return false;
		},

		type: 'AL_checkUserGroup'
	});