
- 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: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:32 AM
Thank you Sir. That's two hours of my life that I won't get back....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 03:35 AM
Glad to help.
Happy learning.
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:26 AM - edited 10-10-2023 03:29 AM
Hi @Andrew Bettcher ,
May I know why your for loop is ending after query command i think that is causing the issue. Can you try removing that n place it at the end & check plz.
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);
}
}
Thanks,
Danish