How to return GlideAjax value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 11:46 PM
Hi, I have UI Action where need to display alert of based on resp value. But the problem cant get the value from mycallback function, Here my script :
function closeComplete(){
if (checkFile() == "1"){
alert("reason A");
} else if (checkFile() == "0"){
alert("reason B");
}else{
return false;
}
}
function checkFile() {
sys_id = g_form.getUniqueValue();
var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sys_id', sys_id);
ga.getXML(mycallback);
function mycallback(response) {
var resp = response.responseXML.documentElement.getAttribute('answer');
return resp;
}
return mycallback;
}
Is there any way to get the value of resp and return to function closeComplete?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 12:17 AM - edited 05-16-2024 12:19 AM
Hi @Muhammad Arif B,
function checkFile() {
var sysID = g_form.getUniqueValue();// variable was not declared
var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sysparm_sys_id', sysID);// it should always start 'sysparm' and also avoid predefined keyword in snow like sys_id and change the sysparm_sys_id in script include where you getting the value this.getParameter('sysparm_sys_id');
ga.getXML(mycallback);
function mycallback(response) {
var resp = response.responseXML.documentElement.getAttribute('answer');
return resp;
}
return mycallback;
}
Please try this code as per updated one.
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up
Thanks
Jitendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 01:01 AM
Hi @Muhammad Arif B ,
try :
function checkFile(callback) {
var sys_id = g_form.getUniqueValue();
var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sys_id', sys_id);
ga.getXML(function(response) {
var resp = response.responseXML.documentElement.getAttribute('answer');
callback(resp);
});
}
checkFile(function(response) {
console.log(response);
});
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 01:40 AM
Hi @Muhammad Arif B
You cannot return from an asynchronous call inside a synchronous method
You need to bring you if/else statement to "mycalback" function
function mycallback(response) {
var resp = response.responseXML.documentElement.getAttribute('answer');
if (resp == "1"){
alert("reason A");
} else if (resp == "0"){
alert("reason B");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 04:07 AM
Hi @Muhammad Arif B,
function closeComplete(){
if (checkFile() == "1"){
alert("reason A");
} else if (checkFile() == "0"){
alert("reason B");
}else{
return false;
}
}
function checkFile() {
sys_id = g_form.getUniqueValue();
var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sys_id', sys_id);
ga.getXML(mycallback);
function mycallback(response) {
var resp = response.responseXML.documentElement.getAttribute('answer');
alert('Script inlcude answer' + resp); //check what is returning
return resp;
}
}
Note: no need to write return mycallback; as you are already retuning resp and after Return no code will run so try the above and let me know if it doesnt work ?
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang