how do i push values in array for a record

Priya75
Tera Contributor

Hi All, 

i want to puch Ip address for the ci , which has 3 records in it. I want 3 ip to be puched to the array in one go.

this is giving me 3 records or if i print it outside, it takes only one record.

 var ciSys_id;
var array=[];
var IP=new GlideRecord("cmdb_ci_ip_address");
//IP.addEncodedQuery("netmask!=64^ORnetmask=NULL^last_discoveredRELATIVEGT@dayofweek@ago@3^nic.cmdb_ci!=NULL");
IP.addQuery("nic.cmdb_ci", "8e01285f1bc4d990635f9714b24bcbac");
//IP.setLimit(5);
//IP.groupBy("nic.cmdb_ci");
IP.query();
while(IP.next())
    { 
    ciSys_id=IP.nic.cmdb_ci;

array.push(IP.ip_address);
gs.print("sys_id"+ciSys_id+array);
}

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

while pushing into array use toString()

var array = [];

var IP = new GlideRecord("cmdb_ci_ip_address");

//IP.addEncodedQuery("netmask!=64^ORnetmask=NULL^last_discoveredRELATIVEGT@dayofweek@ago@3^nic.cmdb_ci!=NULL");

IP.addQuery("nic.cmdb_ci", "8e01285f1bc4d990635f9714b24bcbac");

//IP.setLimit(5);

//IP.groupBy("nic.cmdb_ci");

IP.query();

while(IP.next()){ 
	var ciSys_id = IP.nic.cmdb_ci;
	array.push(IP.ip_address.toString());
}

gs.info("array is" + array);

Regards
Ankur

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

I have 3 records for 

nic.cmdb_ci

, but i want to push IP address  once in array, So 3 ip address for one ci .how can i do that.

Hi,

what's the business requirement?

how would you know when to push value into array?

regards
Ankur

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

Pavankumar_1
Mega Patron

Hi,

 

Try below script and please use gs.info or gs.log to check the values

Ex: gs.log('sys id ' +ciSys_id );

        var ciSys_id;
        var ciarray = [];
        var grci = new GlideRecord('cmdb_ci_ip_address');
        grci.addQuery("nic.cmdb_ci", "8e01285f1bc4d990635f9714b24bcbac");  //filter proper records
        grci.query();
        while (grci.next()) {

            ciSys_id = grci.nic.cmdb_ci;
            ciarray.push(grci.ip_address);
            
        }
        gs.info("sys_id" + ciSys_id + ciarray[0]);
        gs.info("sys_id" + ciSys_id + ciarray[1]);
        gs.info("sys_id" + ciSys_id + ciarray[2]);

 

 

 

Hope it helps!!

Please Mark Correct if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar