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

GlideRecord setting variable outside of If Statement

kemmy1
Tera Guru

I've been a developer for 8+ years, and apparently I missed this in ServiceNow 101.

 

Am I going crazy?  why do I get the last nds_code when I'm just capturing the nds_code if number = G280002292

 

var ndsCode = '';

var gr = new GlideRecord(<tablename>);
gr.addEncodedQuery('lookup_client_last_name=PATRICK');
gr.query();
while(gr.next()){
    if(gr.number == 'G280002292'){
   ndsCode= gr.nds_code;
   gs.info(ndsCode);  //correct data

   }
}
gs.info(ndsCode); //captures data on the last record

 

I get the correct ndsCode captured within the IF statement but then out of the statement I getting the last ndscode

It's setting ndsCode variable whether number is G280002292 or not.

 

Is this as designed?  

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

If the field you are assigning to the variable is not a string, you are storing the object memory location.  Since you are not exiting the while loop after the record is found and the script variable updated, it's using the value of the last record retrieved.  You can force the retrieved to a field, or the safer/surer way if getting the value like this:

 

ndsCode = gr.nds_code.toString();
-or-
ndsCode = gr.getValue('nds_code');

If you are using a script like this beyond a demonstration of the issue, it would be more efficient if you added the number field and value to the query/encoded query before retrieving records, and/or breaking out of the while loop when the record is found.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

If the field you are assigning to the variable is not a string, you are storing the object memory location.  Since you are not exiting the while loop after the record is found and the script variable updated, it's using the value of the last record retrieved.  You can force the retrieved to a field, or the safer/surer way if getting the value like this:

 

ndsCode = gr.nds_code.toString();
-or-
ndsCode = gr.getValue('nds_code');

If you are using a script like this beyond a demonstration of the issue, it would be more efficient if you added the number field and value to the query/encoded query before retrieving records, and/or breaking out of the while loop when the record is found.

Thank you.  toString() worked.   I'm still very confused why it would update my variable when the IF statement is not equal to G280002292.  This is a string field on the backend.  I know I have to do this with an array.push(field.toString()).  

 

But thank you (everyone) this fixed my issue.

Amit Verma
Kilo Patron
Kilo Patron

Hi @kemmy1 

 

I tried reproducing your problem with the incident table and I am able to do it. On further debugging, I can tell you that even though you put an if statement to scope the variable and get the corresponding value, inside the while loop, it still takes the reference to the last record being iterated unless you break the loop or you concatenate the output to an externally defined variable and return it. Refer below scenarios which I tested -

 

1. Defining the ndsCode variable outside and using break statement inside if :

AmitVerma_0-1728269461085.png

 

Output -

AmitVerma_1-1728269476059.png

2. Defining the ndsCode variable outside and concatenating the variable inside if :

AmitVerma_2-1728269550191.png

Output -

AmitVerma_3-1728269564123.png

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Moin Kazi
Kilo Sage
Kilo Sage

Hi @kemmy1 

 

I gone through your code and noticed here in Javascript reference variable concept has been used.

actually in ndsCode variable you are passing variable by reference which value has been changing on every iteration because of that reason in the end you get last record number in your variable. but if you want your value won't change so simply you can use toString() method so your variable hold the string value not the reference.

 

See below -

 

var ndsCode = '';

var gr = new GlideRecord(<tablename>);
gr.addEncodedQuery('lookup_client_last_name=PATRICK');
gr.query();
while(gr.next()){
    if(gr.number == 'G280002292'){
   ndsCode= gr.nds_code.toString();
   gs.info(ndsCode);  //correct data

   }
}
gs.info(ndsCode); //it will give same info as inside loop

 

Please mark this response as correct or helpful if it assisted you with your question.

 

Regards

Moin