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

DrewW
Mega Sage
Mega Sage

In your _getFloorLevel function add console.log(response).  Then look at way is in the browser console.  I think you will find that response is actually an object and logging it to the console will allow you to look thru the object and its values to find what you need.

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

that's because the variable is just initialized and not assigned any value

when you place it inside the callback method you are assigning the response from script include inside it and hence it shows the value

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

    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);

    function _getFloorLevel(response){
        if(response !=''){
            alert(response); // should give you what script include returns
            flLevel = response ; // it is a String value that comes from server as 'Level1'
        }else{
            g_form.addErrorMessage('No value has returned from server!');
        }
    }
}

Regards
Ankur

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

@Ankur Bawiskar 

I need the value returned from server to be assigned to a kind of local variable say 'flLevel '.
and use it in later stages of the onchange client script.

and  I don't want to set any form variable within the callback function using g_from.setValue() etc.

is there any way I would be able to do it?
please advice

@Chuck Tomasi can we set variables (that are declared outside the call back function)within the call-back function body of getXMLAnswer():  please refer to the question code e.g. flLevel.
 and use them within the scope of say onsubmit client script. if so please explain how and if not please let me know the reason. 

So you want a global client side variable and you want an onChange script to set the value of that var.

You need to declare flLevel outside the onChange function or add an onLoad function that does nothing and put the var outside that function.

Another option would be to use the getClientData and setClientData to store the information

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/c_GlideUserAPI#r_GlideUser-getClientData_string?navFilter=glideU