How to return the value from Callback function

ramesh_r
Mega Sage

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)
	
	
}
9 REPLIES 9

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
    }
}

AbhishekGardade
Giga Sage

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

Thank you,
Abhishek Gardade

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

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Kindly check the field on change of which you want to run your client script.

Regards,

Munender