GlideAjax - return a value from callBack

poyntzj
Kilo Sage

I have a script in a catalog item that calls a function

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	checkNameSpace();
   
}

the function is another Client script that runs onload and allows the function to be called - see below

Right now I am passing the variable strField to the callback and doing some processing there - which works, but it would be a lot easier for what I am doing to use some code like the below where I simply return a true/false and do something from the calling script.
(so this is a shared function and editing this for a number of target fields is quite simply bonkers when the calling script above, can get a true/false and do the work.  It keeps the script below simple and quicker)

I am however stumped at how to get that true / false from the callBack

function onLoad() {
}

checkNameSpace = function() {
	var strField = 'tenant_namespace';	
	g_form.hideFieldMsg(strField,true);
	var strName = g_form.getValue(strField)+'-' +g_form.getValue('tenant_environment')+'';
	if(strName.length < 6) {
             return true;
	}

	var ga = new GlideAjax('ValidationsAjax');
	ga.addParam('sysparm_name','checkNameSpace');
	ga.addParam('sysparm_environment',g_form.getValue('tenant_environment')+'');
	ga.addParam('sysparm_namespace',g_form.getValue(strField)+'');
	ga.getXMLAnswer(function(response){processNS(response,strField);});


	function processNS(response,strField) {
		try {
			var answer = response;
			if(answer =='true' || answer == true) {
				// Right now, I am doing some more work here on the strField  - I want to remove
				return true;
			} else {
				return false;
				// Right now, I am doing some more work here on the strField  - I want to remove
			}

		} catch(e) {
			console.log('check ns error : ' + e);
		}
	}

};


 

Any ideas if it can be done. I have not found anything

 

11 REPLIES 11

paulmorris
Giga Sage

Because your processNS function is asynchronous, your calling code is not able to receive a return value from it.

In other words, checkNameSpace() returns before the callback response function is executed. So you can't return a value.

For this to work, your GlideAjax needs to be synchronous using getXMLWait (this will block the browser) OR you need to handle the true/false value within the callback.

Makes sense?

Hi Paul,

Think you are right. I have updated the script. If we pass the callback along to the processNS, will that work?

Yeah, that should work.

Willem
Tera Sage

Hi,

Hope you are doing well.

Is your question resolved? Or do we need to follow-up on this?

Please mark the answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,

Willem

Hi Willem

Got a little bust here last week and this week is the same - though I am working on this item again. I need to get it ready for a release which at this moment in time means I need to go with the existing code.  If I finish in time, I will look to switch or wait for any future updates for it.