
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:15 AM
Hi,
I'm following this post (which I also commented on saying something like "Wow. It worked!!") to create a simply array that iterates through a list of choices and performs a glide record query using each of the items in an array like this:
var deviceTypes = [
'Laptop','SharePoint','PC'
];
for(var i in deviceTypes) {
var ciDevices = new GlideRecord('u_cmdb_ci_devices');
ciDevices.addEncodedQuery('u_device_type=' + deviceTypes[i]);
ciDevices.query();
}
while(ciDevices.next()) {
gs.print(ciDevices.u_device_type);
}
The problem is that the output (gs.print) just gives me one device type meaning that it isn't iterating through my array to perform the queries OR, it is but it's simply forgetting everything when it does the next one (i.e. because if I change the order of the array, it always prints the last one). There are definitely records that match all 3 device types and so I thought I'd get a ling list of device types (as many as there are records) but I don't.
I get this:
What am I doing wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:25 AM
the while should be within FOR
var deviceTypes = [
'Laptop','SharePoint','PC'
];
for(var i in deviceTypes) {
var ciDevices = new GlideRecord('u_cmdb_ci_devices');
ciDevices.addEncodedQuery('u_device_type=' + deviceTypes[i]);
ciDevices.query();
while(ciDevices.next()) {
gs.print(ciDevices.u_device_type);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:27 AM
Hi,
Can you please try below:
var deviceTypes = [
'Laptop','SharePoint','PC'
];
for(var i in deviceTypes) {
var ciDevices = new GlideRecord('u_cmdb_ci_devices');
ciDevices.addEncodedQuery('u_device_type=' + deviceTypes[i]);
ciDevices.query();
while(ciDevices.next()) {
gs.print(ciDevices.u_device_type);
}
}
Your while loop was outside for loop so it was only printing last choice record valiues.
Thanks
Anil Lande