How to return value to UI action button from script include

brysonelson
Kilo Contributor

Hello,

I am using a client UI Action button to call a script include, which is working fine. However, the callback for getXML is not firing as I'm not returning a value. The reason I'm not returning a value is because the script include hits one method, which in turn calls another method in the script include, and that method then calls another.

So I have three methods in the script include daisy chained together, and I cant figure out how to pass the value back to the UI Action.

My end goal is to get the getXML callback to fire, so I can reload the form via a script in the UI Action

There is some sample code below to demonstrate what I have going on:

P.S. This is very, very dumbed down code just to protect what I'm building. All the methods are getting hit in my real code, I just need the handleResponse callback to fire after all three methods run.

Thanks

UI Action:


function testFunction() {
	//alert("You Clicked Me!");
	var ga = new GlideAjax('MoxyiUtils');
	ga.addParam('sysparm_name', 'getScript');
	ga.addParam('sysparm_serial_number', g_form.getValue('serial_number'));
	
	ga.getXML(handleResponse);
}

function handleResponse(response) {
	console.log("Getting a response " + response);
	var userAnswer = response.responseXML.documentElement.getAttribute('answer');
	var answer = JSON.parse(userAnswer);
	reloadWindow(window);
	
// 	console.log(answer);
	//alert(answer);
}

Script Include:


var MoxyiUtils = Class.create();
MoxyiUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	//initialize: function() {
		//},
		
		
		getScript: function() {
			//store the serial number that was passed from the client
			var serial = this.getParameter('sysparm_serial_number');
			
			json = {
                          serialNum: serial
                        };

			this.setScript(json);
			
		},
		
		setScript: function(json) {
			
			//store the variables passed in from the function call
			
			var serial = json.serialNum;
			
			
			this.assignScript(serial);
		},
		
		assignScript: function(asset) {
			
			//query the hardware table so we can get the sys_id of the asset
			var assetSysId = new GlideRecord('alm_hardware');
			assetSysId.addQuery('serial_number', asset);
			assetSysId.setLimit(1);
			assetSysId.query();
			
			var assetID = '';
			
			//store the sys_id
			while(assetSysId.next()) {
				//gs.debug(assetSysId.sys_id);
				assetID = assetSysId.sys_id;
			}
			
			
			return null;
		},
		
		
		type: 'MoxyiUtils'
	});
1 ACCEPTED SOLUTION

Sebastian R_
Kilo Sage

Each of your methods have to return a value. The value returned by getScript is then available in the getXML Callback function.

 

assignScript: --> return assetID

setScript  --> return this.assignScript(serial);

getScript --> return this.setScript(json);

View solution in original post

5 REPLIES 5

Sebastian R_
Kilo Sage

Each of your methods have to return a value. The value returned by getScript is then available in the getXML Callback function.

 

assignScript: --> return assetID

setScript  --> return this.assignScript(serial);

getScript --> return this.setScript(json);

AbhishekGardade
Giga Sage

hello

Have you marked script include as client callable?

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Hello brysonelson,

I can see you are returning a null value, why?

Why are you complicating the script include ? You can simply optimize a code a below:

UI ACTION:

function testFunction() {
//alert("You Clicked Me!");
var ga = new GlideAjax('MoxyiUtils');
ga.addParam('sysparm_name', 'assignScript');
ga.addParam('sysparm_serial_number', g_form.getValue('serial_number'));

ga.getXML(handleResponse);
}

function handleResponse(response) {
console.log("Getting a response " + response);
var userAnswer = response.responseXML.documentElement.getAttribute('answer');
alert(userAnswer);
reloadWindow(window);
// console.log(answer);

}

 

 

SCRIPT INCLUDE:

 

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

assignScript: function() {
var serial = this.getParameter('sysparm_serial_number');
//query the hardware table so we can get the sys_id of the asset
var assetSysId = new GlideRecord('alm_hardware');
assetSysId.addQuery('serial_number', serial);
assetSysId.setLimit(1);
assetSysId.query();

var assetID = '';
if(assetSysId.next()) {
//gs.debug(assetSysId.sys_id);
assetID = assetSysId.sys_id.toString();
}


return assetID;
},

type: 'MoxyiUtils'
});

 

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are calling getScript from Ajax;

from getScript you are calling setScript and from that you are calling assignScript

Why so many calls and not directly call assignScript?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader