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

My actual code uses an external API call in getScript, then passes that information to setScript which stores the returned data, and assignScript uses some info from both functions.

Also those are not the real names of the methods, I just simplified them for the question.

It doesn't make much sense with this dumbed down code, but it makes more sense if you look at the actual script.