Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Can you try using getXMLAnswer() and see if it works

https://docs.servicenow.com/bundle/london-application-development/page/app-store/dev_portal/API_reference/GlideAjaxV3/concept/c_GlideAjaxV3API.html#r_GLAXV3-getXMLAnswer_F

Not working, still returning null values.

Raj68
Mega Guru

Hi,

Please try the code after replacing double quote to single quote like below:

 

Line17: ("answer") should be in single quote like this ('answer').

 

NOTE: Mark correct or helpful if it helps you.

 Warm Regards,

Raj patel

 

That doesn't makes a difference, since both represent a string. Still I tried and again there is null output.

bishopx
Giga Guru

I don't know your data structure, but this could be a problem.

modelname = model.model_id.display_name;

Do you want to get value of the field name "display_name" from the reference of the record in "model_id" field?
Or did you want to get the display name of the "model_id" field ? If yes, then you need to use getDisplayValue() function:

modelname = model.model_id.getDisplayValue();

I have encountered multiple instances of ServiceNow not return any error in the Client Script include to the log, and instead returning null to client.

Let me know

Jozef