How to return the value from Callback function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 02:49 AM
Hi All,
I have client script with GlideAjex and it's returning the value properly
I want to get out the callback function return value but it's not working
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AjaxScriptInclude');
ga.addParam('sysparm_name', 'greetingFunction');
ga.addParam('sysparm_my_name', "Tim");
ga.getXML(myCallBack);
var val;
function myCallBack(response) {
var greeting = response.responseXML.documentElement.getAttribute('answer');
val= greeting; // This is returns correct value
}
alert(val); // i want to use the value here (Out side the function)
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 04:40 AM
Well just do the alert in the callback function then
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AjaxScriptInclude');
ga.addParam('sysparm_name', 'greetingFunction');
ga.addParam('sysparm_my_name', "Tim");
ga.getXML(myCallBack);
function myCallBack(response) {
var greeting = response.responseXML.documentElement.getAttribute('answer');
alert(greeting); // This is returns correct value
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 03:08 AM
try this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AjaxScriptInclude');
ga.addParam('sysparm_name', 'greetingFunction');
ga.addParam('sysparm_my_name', "Tim");
ga.getXML(myCallBack);
}
var val;
function myCallBack(response) {
var greeting = response.responseXML.documentElement.getAttribute('answer');
val= greeting; // This is returns correct value
}
alert(val); // i want to use the value here (Out side the function)
Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 04:17 AM
Hi Thanks,
Thanks for the response I tried the code but the alert is showing when a form is loading with undefined value and not working the Onchange also
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 04:29 AM
Hi Ramesh,
you will get undefined for the val when you do alert.
What is your exact requirement here? why not use the variable greeting inside the callback and do whatever validation etc you want to do
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2019 04:30 AM
Hi Kindly check the field on change of which you want to run your client script.
Regards,
Munender