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:26 AM
Try this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var returnValue = checkNameSpace();
if(returnValue == true){// or just if(returnValue){}
}
else{
}
}
And script include:
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) {
return 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);
}
}
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 06:05 AM
I am sure this is on the methods I have tried but failed with
Trying to set a variable for the callBack and then returning that fails too as it tried to return "j" before the callback as finished
var j = ga.getXMLAnswer(function (response) {
return processNS(response, strField);
});
return j;
getXMLWait can be so useful at times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 06:27 AM
Been looking at Promise, but failing there too.
Alas I have to finish what I am doing so will have to keep the work around in place, but I may look at again at some stage
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2020 05:40 PM
You could try it with a callback function:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
checkNameSpace(callbackNameSpace);
}
function callbackNameSpace(boolOutcome) {
if (boolOutcome) {
//
}
else {
}
}
function onLoad() {
}
checkNameSpace = function (callback) {
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, callback)
});
function processNS(response, strField, callback) {
try {
var answer = response;
if (typeof callback === "function") {
callback(answer);
}
} catch (e) {
console.log('check ns error : ' + e);
}
}
};