- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 02:05 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 02:46 PM - edited 10-06-2024 03:27 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 02:46 PM - edited 10-06-2024 03:27 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2024 05:42 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 07:57 PM
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 :
Output -
2. Defining the ndsCode variable outside and concatenating the variable inside if :
Output -
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 11:14 PM
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