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

HI Karthik, 

 

Yes, That should work. I need to glide another table after this and populate a field with this value.

Regards,

Priya

Hi @Priya 

Please check the update code below:

var ciSys_id;
var obj = {};
var IP = new GlideRecord("cmdb_ci_ip_address");
//Add GroupBy clause in query or we can add IP.groupBy("nic")
//nic.cmdb_ci will not work
IP.addEncodedQuery("netmask!=64^ORnetmask=NULL^nic.cmdb_ci!=NULL^GROUPBYnic");
IP.query();
while (IP.next()) {
    ciSys_id = IP.nic.cmdb_ci;
    if(JSUtil.nil(obj[ciSys_id])) {
        obj[ciSys_id] = [];
    }
    gs.print(ciSys_id);
    obj[ciSys_id].push(IP.ip_address.toString());
}

gs.print(JSON.stringify(obj));

This will work for you!

Screenshot:

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Could you please explain me  the code . I need to use this here .

 

Can we do it by array as well? just a question .

    var gr=new GlideRecord("cmdb_ci_server");
    gr.addQuery("sys_id",ciSys_id);
    gr.query();
     while(gr.next()){
        
        gr.u_associated_ip_addresses= array.ip_address;
        gr.update();

Kartik Sethi
Tera Guru
Tera Guru

Hi @Priya 

 

The JSON object will look like as provided below:

{
  "a9a2d111c611227601fb37542399caa8": [
    "198.162.0.1",
    "198.162.0.2",
    "198.162.0.3"
  ],
  "b4fd7c8437201000deeabfc8bcbe5dc1": [
    "172.108.0.2",
    "172.108.0.1"
  ]
}

So you need to pass the object without converting it to string and the final code will be:

var ciSys_id;
var obj = {};
var IP = new GlideRecord("cmdb_ci_ip_address");
//Add GroupBy clause in query or we can add IP.groupBy("nic")
//nic.cmdb_ci will not work
IP.addEncodedQuery("netmask!=64^ORnetmask=NULL^nic.cmdb_ci!=NULL^GROUPBYnic");
IP.query();
while (IP.next()) {
    ciSys_id = IP.nic.cmdb_ci;
    if(JSUtil.nil(obj[ciSys_id])) {
        obj[ciSys_id] = [];
    }
    gs.print(ciSys_id);
    obj[ciSys_id].push(IP.ip_address.toString());
}

//gs.print(JSON.stringify(obj));


for(var id in obj) {
	var gr=new GlideRecord("cmdb_ci_server");
	gr.addQuery("sys_id",id);
	gr.query();
	while(gr.next()){
		gr.u_associated_ip_addresses= obj.id;
		gr.update();	
	}
}

If my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.

Thanks and regards,

Kartik

hi ,

 

Last pasrt is not working as expected .

 i am getting garbage values,

gr.u_associated_ip_addresses= obj.id;