How to create an array of arrays in javascript from gliderecord results

anfield
Tera Guru

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]);

}


1 ACCEPTED SOLUTION

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?

View solution in original post

13 REPLIES 13

Yes the function x_days_or_older does return the array

I don't see any issues with that loop. What is the output when you run that?

It just says undefined about 300 times, one for each record

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?

Yes the field names are correct