The CreatorCon Call for Content is officially open! Get started here.

How do i pass value to a callback function

Ravish Shetty
Tera Guru

Hello all,

Is there a way to pass value to the callback function when we use GlideAjax.

function onChange (){

var temp = '123';

var x = new GlideAjax ('ABC');

x.addParam ('sysparm_name','123');

x.getXML(callBack,temp);

}

function callBack (response,temp){

alert(temp);

}

Here i want to pass the value of variable temp to the callback function without declaring it as a global variable.

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hello Ravio,



You can do it   with a closure:



function onChange(){


        var temp = '123';


        var x = new GlideAjax('ABC');


        x.addParam ('sysparm_name', '123');


        x.getXML( function (response) { callback (response, temp); });


}



function callBack (response, temp){


        alert(temp);


}


View solution in original post

8 REPLIES 8

@edwin.munoz .  Thank you so much!

The SN Nerd
Giga Sage
Giga Sage

Not being aware of Edwin's method, I would have done this by adding parameter "sysparm_temp" with the variable temp and returned it back in the Ajax script include.



I can post some code if you would find it helpful, although Edwin's solution is much cleaner.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Kalaiarasan Pus
Giga Sage

Is your code not working?As I remember using the way you have specified


Hi Kalai,



The syntax in which i have specified in the question doesn't work.