GlideAjax - return a value from callBack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 04:52 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 05:51 PM
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?
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 05:59 PM
Hi Paul,
Think you are right. I have updated the script. If we pass the callback along to the processNS, will that work?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 08:09 PM
Yeah, that should work.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 03:35 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2020 01:41 AM
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.