GR query results pushed to an array; iteration issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 02:24 PM
Community,
Currently having difficulties with pushing results from a grquery into an empty array properly.
var result = [];
var glideRecord = new GlideRecord (table);
glideRecord.addQuery('parameter', 'string');
glideRecord.query();
while(glideRecord.next()) {
gs.info('found' + notesGr.number);
result.push(glideRecord.number)
gs.info('result: ' + result)
}
the while loop is overwriting the previous query result found
this is what the while loop is doing to the array:
x_snc_createnotes: foundNOTE0001072
x_snc_createnotes: result: NOTE0001072
x_snc_createnotes: foundNOTE0001087
x_snc_createnotes: result: NOTE0001087,NOTE0001087
x_snc_createnotes: foundNOTE0001069
x_snc_createnotes: result: NOTE0001069,NOTE0001069,NOTE0001069
x_snc_createnotes: foundNOTE0001086
x_snc_createnotes: result: NOTE0001086,NOTE0001086,NOTE0001086,NOTE0001086
x_snc_createnotes: foundNOTE0001055
x_snc_createnotes: result: NOTE0001055,NOTE0001055,NOTE0001055,NOTE0001055,NOTE0001055
x_snc_createnotes: foundNOTE0001058
x_snc_createnotes: result: NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 02:30 PM
Hi there,
Please update your script as below:
var result = [];
var glideRecord = new GlideRecord (table);
glideRecord.addQuery('parameter', 'string');
glideRecord.query();
while(glideRecord.next()) {
gs.info('found' + glideRecord.number);
result.push(glideRecord.number);
}
gs.info('result: ' + result.join());
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 02:54 PM
Hey Christopher,
So the issue is the way the while loop is iterating over the result array and overwriting the previous result found
this is what the while loop is doing
x_snc_createnotes: foundNOTE0001072
x_snc_createnotes: result: NOTE0001072
x_snc_createnotes: foundNOTE0001087
x_snc_createnotes: result: NOTE0001087,NOTE0001087
x_snc_createnotes: foundNOTE0001069
x_snc_createnotes: result: NOTE0001069,NOTE0001069,NOTE0001069
x_snc_createnotes: foundNOTE0001086
x_snc_createnotes: result: NOTE0001086,NOTE0001086,NOTE0001086,NOTE0001086
x_snc_createnotes: foundNOTE0001055
x_snc_createnotes: result: NOTE0001055,NOTE0001055,NOTE0001055,NOTE0001055,NOTE0001055
x_snc_createnotes: foundNOTE0001058
x_snc_createnotes: result: NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058,NOTE0001058

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 03:12 PM
Hmm that is a strange one, that certainly isn't how the the Javascript Array.push() method is supposed to work 🙂
That sorta makes me think the fact that you're declaring your GlideRecord object as var glideRecord might be causing some issues because "glideRecord" could be a reserved term in ServiceNow... for example, I have seen similarly weird issues when using var gr = new GlideRecord('table_name');
So a few things to try/check here:
1. Update your variable name to be something which is less likely to be a reserved term, something like notesGr
2. Try using the .getDisplayValue() method on your notesGr object instead... this really shouldn't make a big difference but the getDisplayValue() method is a bit cleaner from a technical perspective because it is guaranteed to return a string type.
3. I am assuming this is already happening w/ some var table = 'table_name'; declaration above what you included in your code snippet, but just to be sure that you are providing a proper table name string value in the notesGr declaration as well.
Updated script:
var result = [];
var notesGr = new GlideRecord('your_table_name'); // replace w/ your table name
notesGr.addQuery('parameter', 'string');
notesGr.query();
while (notesGr.next()) {
gs.info('found' + notesGr.getDisplayValue());
result.push(notesGr.getDisplayValue());
}
gs.info('result: ' + result.join());
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2022 08:46 AM
use
result.push(glideRecord.getValue("number"));
Vinod Kumar Kachineni
Community Rising Star 2022