The CreatorCon Call for Content is officially open! Get started here.

Client callable Script Include returning null values to Client Side UI Action

AirSquire
Tera Guru

Hi I am trying to pull some server side values to client side for use in UI Action(client side) using client callable script include. And getting a null value in the answer. Below are my both of the codes

UI Action(Form button, client, onClick function - commentsDialog())

function commentsDialog() {
	//Get the values to pass into the dialog
	var caller = g_form.getReference('caller_id', doAlert);
	
}
function doAlert(caller) {
	var location = caller.location;
	var ci = g_form.getValue('cmdb_ci');

	var ga = new GlideAjax("TogetModelAndLocationName");
	ga.addParam("sysparm_name", "getName");
	ga.addParam("sysparm_ci", ci);
	ga.addParam("sysparm_loc",location);
	ga.getXML(Callback);
	//alert(ga.getAnswer().toString());
}
function Callback(response){
	//var answer = JSON.parse(response);
	var answer = response.responseXML.documentElement.getAttribute("answer");
	alert(answer);
}

Script Include(client callable)

var TogetModelAndLocationName = Class.create();
TogetModelAndLocationName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getName: function(){
		var modelname = '';
		var locationname = '';
		var name = '';
		var sysId = this.getParameter('sysparm_ci');
		var locsysID = this.getParameter('sysparm_loc');
		var model = new GlideRecord( 'cmdb_ci' );
		model.addQuery('sys_id',sysId);
		model.query();
		if( model.next() ){
			modelname = model.model_id.display_name;
		}
		var location = new GlideRecord('cmn_location');
		location.addQuery('sys_id',locsysID);
		location.query();
		if(location.next()){
			locationname = location.name;
		}
		name = modelname+','+locationname;
		gs.log(name,'air');
		return name;
	},
	
    type: 'TogetModelAndLocationName'
});

When I execute UI Action, it gives me a "null" as alert but the log generated from Script Include gives correct values. I tried JSON encoding of the return value and also tried synchronous call both result in no alerts.

1 ACCEPTED SOLUTION

MartinFRU
Mega Expert

Hi AirSquire,

Client Script:

function commentsDialog() {
	//Get the values to pass into the dialog
	var caller = g_form.getReference('caller_id', doAlert);
	// alert((g_form.getReference('caller_id').name));
	
}
function doAlert(caller) {
	var location = caller.location;
	var ci = g_form.getValue('cmdb_ci');

	var ga = new GlideAjax("TogetModelAndLocationName");
	ga.addParam("sysparm_name", "getName");
	ga.addParam("sysparm_ci", ci);
	ga.addParam("sysparm_loc",location);
	ga.getXML(callback);	
}
function callback(response){	
	var answer = response.responseXML.documentElement.getAttribute("name");	// Changed this
	alert(answer);
}



Include:

var TogetModelAndLocationName = Class.create();
TogetModelAndLocationName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getName: function(){
		var modelname = '';
		var locationname = '';
		var name = '';
		var sysId = this.getParameter('sysparm_ci');
		var locsysID = this.getParameter('sysparm_loc');
		var model = new GlideRecord( 'cmdb_ci' );
		model.addQuery('sys_id',sysId);
		model.query();
		// gs.log('CI count: '+model.getRowCount());
		if( model.next() ){
			modelname = model.model_id.display_name;
		}
		var location = new GlideRecord('cmn_location');
		location.addQuery('sys_id',locsysID);
		location.query();
		gs.log('LOC count: '+location.getRowCount());
		if(location.next()){
			locationname = location.name;
		}
		name = modelname+','+locationname;
		
		this.getRootElement().setAttribute('name', name); // Changed this		
	},
	
    type: 'TogetModelAndLocationName'
});

 

See this post: Glide Ajax returns null from Mahendra.
Quickly tested on my DEV - seems to work fine.

Hope this helps.

Cheers!

Martin

View solution in original post

12 REPLIES 12

Alikutty A
Tera Sage

Hi,

Can you try returning 

return name.toString();

Also when you tried Json, did you encode it?

return JSON.stringify(name);

Thanks!

I tried JSON.encode(name)

What do you get when you alert(response)

[object XMLHttpRequest]