GlideRecord using array values doesn't work

Andrew Bettcher
Kilo Sage

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:

AndrewBettcher_0-1696932852811.png

What am I doing wrong?



1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew Bettcher 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew Bettcher 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you Sir. That's two hours of my life that I won't get back....

@Andrew Bettcher 

Glad to help.

Happy learning.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Danish Bhairag2
Tera Sage
Tera Sage

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