- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 07:39 PM
Hi, I have UI Action where need to display alert of string respStr. But the problem cant get the value from mycallback function, Here my script :
function closeComplete(){
alert(checkFile());
}
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 respStr = response.responseXML.documentElement.getAttribute('answer');
return respStr;
}
return mycallback;
}
Is there any way to get the value of respStr and return to function closeComplete?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 08:31 PM
It looks like you're trying to get the value of respStr from the mycallback function and use it in the closeComplete function. However, due to the asynchronous nature of AJAX requests, you can't directly return a value from mycallback to closeComplete.
Instead, you can modify your code to pass a callback function to checkFile and execute that callback when the response is received.
function closeComplete(){
checkFile(function(respStr) {
alert(respStr);
});
}
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 respStr = response.responseXML.documentElement.getAttribute('answer');
callback(respStr);
});
}
Now, checkFile accepts a callback function as an argument and executes it with respStr as its parameter once the response is received. In closeComplete, you pass an anonymous function to checkFile, which displays the alert with the received respStr.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 07:55 PM
Without insights into your complete implementation and thrown error messages it is difficult to answer but I saw at least one error in your script. Try the following version:
function closeComplete(){
checkFile();
}
function checkFile() {
var 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 respStr = response.responseXML.documentElement.getAttribute('answer');
alert(respStr);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 08:06 PM
Hi @Muhammad Arif B,
I don't believe there is a way to return the value of respStr to closeComplete() as the callback function is executed asynchronously.
You will have to add the alert within the callback function.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 08:18 PM
function closeComplete(){
checkFile();
}
function checkFile() {
var sys_id = g_form.getUniqueValue();
var ga = new GlideAjax('AjaxFSMClosureValidation');
ga.addParam('sysparm_name', 'callFuntionFileValidate');
ga.addParam('sys_id', sys_id);
ga.getXMLAnswer(mycallback);
function mycallback(answer) {
alert(answer);
}
}
Mark it Helpful and Accept Solution!! If this helps you to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2024 08:31 PM
It looks like you're trying to get the value of respStr from the mycallback function and use it in the closeComplete function. However, due to the asynchronous nature of AJAX requests, you can't directly return a value from mycallback to closeComplete.
Instead, you can modify your code to pass a callback function to checkFile and execute that callback when the response is received.
function closeComplete(){
checkFile(function(respStr) {
alert(respStr);
});
}
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 respStr = response.responseXML.documentElement.getAttribute('answer');
callback(respStr);
});
}
Now, checkFile accepts a callback function as an argument and executes it with respStr as its parameter once the response is received. In closeComplete, you pass an anonymous function to checkFile, which displays the alert with the received respStr.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks