local variable on an onChange client script is not populated via GlideAjax call even a value comes from the server

Dush
Tera Contributor

Hi Team
I'm developing an Scope Application where there is a table called Cabinet.
I have written a onchange client script and calling a field value of another reference table using a GlideAjax call.
Note : Script include function correctly and it returns the level value as expected e.g. Level 1

on change client script in scoped application

function onChange(control,oldValue,newValue,isLoading,isTemplate){
if(isLoading || newValue ==''){
return;
}

var flLevel;
var floorSysID = g_form.getValue('dc_floor'); //sys_id of the reference table record

var fl = new GlideAjax('DCFloorRecord');
fl.addParm('sysparm_name','getDCFloorLevel');
fl.addParm('sysparm_dcFloorSysId',floorSysID);
fl.getXMLAnswer(_getFloorLevel);

alert(flLevel);

function _getFloorLevel(response){
if(response !=''){
flLevel = response ; // it is a String value that comes from server as 'Level1' 

}else{
g_form.addErrorMessage('No value has returned from server!');
}


}

issue:

flLevel variable is not assigned and the alert gives undefined as the out put. But if I print the very same variable in side the call back function the variable is well set.


please can some one share his wisdom as to why this strange behavior is displayed.
Where  my flaw resides? 😞


My whole attempt is to set the flLevel with what server provides ( e.g. 'Level 1').



5 REPLIES 5

Jorn van Beek
Tera Guru

the "Issue" is becasue the ajax call runs async.

this means the main function will continue to run without waiting for an answer. Therefore when you try to use the variable later on in the on-change script it is undefined, the ajax call hasnt returned the value (jet).

if you want to use the variable later on in the script, I would place the logic within the callback function.