how do i push values in array for a record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2022 11:08 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2022 12:09 AM
HI Karthik,
Yes, That should work. I need to glide another table after this and populate a field with this value.
Regards,
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2022 12:49 AM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2022 01:16 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2022 01:34 AM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2022 01:56 AM
hi ,
Last pasrt is not working as expected .
i am getting garbage values,
gr.u_associated_ip_addresses= obj.id;