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

ARG645
Tera Guru
Below are some things i noticed, these may or may not be culprits - In your script include you are returning gr.u_priority which is an Object. Use return gr.getValue(‘u_priority’) - your custom table (u_ncr_matrix) , you are querying the subcategory with the Category. Is this the right way of querying the table?

Hi Aman,

 

I think I should clarify that I am unable to get anything out of the script include based on what is in the client script. I modified the return as suggested, and also added a gs.addInfoMessage('test') which never appeared. So I don't think that the script include is actually being called at all.

 

My custom table is a very basic table, it stores a category, subcategory, and priority. I use the category/subcategory from the client script to return the priority. I could have easily made a massive if/else block in the client script but I believe it would be better to have this table instead of dumping everything in the client script.

 

 

Brent Sutton
Mega Sage

In your script include you are querying "u_subcategory" with the "category" variable. Should be changed to the following:

var AL_SCRIPT = Class.create();
AL_SCRIPT.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getPriority:  function () {
		var category = this.getParameter('sysparm_category');
		var subcategory = this.getParameter('sysparm_subcategory');
		var priority = "";
		var gr = new GlideRecord('u_ncr_matrix');
		gr.addQuery('u_category', category);
		gr.addQuery('u_subcategory', subcategory);
		gr.query();
		if(gr.next()){
			priority = gr.getValue('u_priority');
		}
		return priority;		
	},

	type: 'AL_SCRIPT'
});

I would also simplify the client script by using the following:

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.getXMLAnswer(parseAjax);	
}

function parseAjax(answer) {
	g_form.addInfoMessage('priority: ' + answer);
	if(answer){		
		g_form.setValue('priority',answer);
	}
}

Let me know if this worked for you.

Thanks

Brent

Hi Brent,


Thanks for picking that up, I did realise this as well a few minutes ago but changing it did not fix the issue. Since the priority variable is initialised as an empty string, the info message in my client script should have printed an empty string, but instead it is printing null. So the problem to me looks like it sits with var answer = response.responseXML.documentElement.getAttribute("answer"); as it is unable to retrieve the actual variable that the script include is trying to send.

edit: Sorry I mixed some things up, the field that is being returned is either the priority or false, so the info message in the client script will either print a number or false, but it is printing null. 

Did you try it with my simplified client script?

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.getXMLAnswer(parseAjax);	
}

function parseAjax(answer) {
	g_form.addInfoMessage('priority: ' + answer);
	if(answer){		
		g_form.setValue('priority',answer);
	}
}