- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2020 11:32 AM
Running a fix script which returns about 300 records, each record will return 4 fields. What I want to do is put the entire contents in one array, and then for each record put that into an individual array. I know what I have below doesnt work but I wanted some guidance on how to accomplish this?
I am creating my first array before the glide record while, based on the number of records returned, but I am trying to figure out how to populate it. The 2 nested for loops below should be fine to iterate over the data, but I need to build the nested array first. I know how to add the data into an ordinary array with using push, but its the nested part thats giving me trouble.
The arrays should look like this:
old_computers = [
[field1 ,field2 ,field3 , field4],
[field1,field2,field3,field4]
];
I will get the field data after the glide record query like this:
var compname = grw.getValue('name');
var serial = grw.getValue('serial_number');
Fix Script:
function x_days_or_older(){
var grw = new GlideRecord('cmdb_ci_computer');
grw.addQuery('sys_class_name','=', 'cmdb_ci_computer');
// Not equal to decommissioned
grw.addQuery('operational_status', '=', 5);
grw.addQuery('discovery_source','=', 'MS SMS');
grw.addQuery('u_sccm_last_scan','!=','');
grw.addEncodedQuery('sys_class_name=cmdb_ci_computer^u_sccm_last_scanRELATIVELT@dayofweek@ago@500');
grw.query();
gs.log("sccm last scan number: " + grw.getRowCount());
var old_computers_all = new Array (grw.getRowCount());
while (grw.next()) {
for (var i=0; i < old_computers_all.length; i++){
for (var j=0; j < old_computers_all[i].length; j++);{
gs.log('SCCM olderscans: inner loop: ' + old_computers_all[i][j]);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2020 07:03 AM
In your original loop change it to:
while (grw.next()) {
var tempArray = []
tempArray.push(grw.getValue('name') + '');
tempArray.push(grw.getValue('serial_number') + '');
old_computers_all.push(tempArray);
}
Are you sure your field names are correct?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2020 11:36 AM
If I'm following what you're attempting to do, your loop would look like:
while (grw.next()) {
var tempArray = []
tempArray.push(grw.getValue('name'));
tempArray.push(grw.getValue('serial_number'));
old_computers_all.push(tempArray);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2020 01:49 PM
Thanks that does help. I was trying to pass the results of that first function to a second function though and just iterate through the results and I'm just seeing undefined (for each record). Do my two for loops below look correct? I've confirmed that I am seeing the correct number of records when I log the length before the for loop but I'm not sure why the loop below isnt working.
function unpack() {
gs.log('SCCM olderscans inside unpack: ');
var old_computers_all = x_days_or_older();
gs.log('SCCM Olderscans: Unpack: length: ' + old_computers_all.length);
for (var i=0; i < old_computers_all.length; i++){
for (var j=0; j < old_computers_all[i].length; j++);{
gs.log('SCCM olderscans: inner loop: ' + old_computers_all[i][j]);
}
}
}unpack();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2020 04:30 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2020 12:06 AM
use OOTB function to get confirm whether it returns array or not as follow.
Array.isArray(x_days_or_older()); if it returns true then we are good.