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

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.

Kartik Sethi
Tera Guru
Tera Guru

Hi Priya,

 

Could you please elaborate on your requirement?

 

The solution provided by @Ankur Bawiskar should work for you, it seems you still have a bottleneck so please provide more details.

 

As per my understanding, you have 1 CMDB CI record -- A, but it is associated with more than one IP Address records (P, Q, R,...)

You want to push the IP addresses of all the matching records in an array. What exactly do you mean by:

but i want to push IP address  once in array,

Thanks and regards,

Kartik

yes, For 1 ci record there are more than one Ip address associated with it, and if i use it in while loop , It stores one value each time. If i push these value outside it puch only one value.

 

I want these 3 values of ip address to be pused once in the array , and i have more than one CI record.

 

 

 Script: sys_id8e01285f1bc4d990635f9714b24bcbac10.244.71.68
*** Script: sys_id8e01285f1bc4d990635f9714b24bcbac10.244.71.68,192.168.35.101
*** Script: sys_id8e01285f1bc4d990635f9714b24bcbac 10.244.71.68,192.168.35.101,10.244.71.4
// i want this result so that i could populate it in other table.



Just make sure to use 

 

array.push(IP.getValue('ip_address')); as directly using GlideRecord reference causes problem in arrays as it stores references of last record only hence only the last value comes 3 times

Got your requirement!

 

Let's say there 3 netmask CMDB CI - A, B, C, and these 3 CIs have IP addresses (P, Q, R, X, Y, M, N) as provided below:

  • A - P, Q, R
  • B - X, Y
  • C - M, N

The final result required is:

sys_id<A>: [P, Q, R]

sys_id<B>: [X, Y]

sys_id<C>: [M,N]

Can we go for JSON object rather than just Array?

 

Please confirm.

 

Thanks and regards,

Kartik