- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 01:25 AM
Hi All,
I want to copy all the values returned from a while loop in a string field.Example if while loop gives me 3 ips, then ipdup field should have 1,2,3 ips.
var record=arr[i];
var gr= new GlideRecord("cmdb_ci_ip_address");
gr.addQuery("nic.cmdb_ci",record);
gr.query();
while(gr.next()){
ip=gr.ip_address.toString();
ipArr.push(ip.toString());//this is giving the first value once, second value twice and third one thrice.
}
}
for(var j=0;j<ipArr.length;j++){
ipArr[i];
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 01:44 AM
Hello,
Please try below script to achieve your requirement i have pushed it to any array called ipADD and you an assign that array to your string field which will populate IPS in comma seperated values
var ipADD=[];
var gr= new GlideRecord("cmdb_ci_ip_address");
gr.addQuery("nic.cmdb_ci",record);
gr.query();
while(gr.next()){
ipADD.push(gr.ip_address.toString());//this is giving the first value once, second value twice and third one thrice.
}
Please mark my answer correct if it helped you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 01:30 AM
Hi Madhu,
You can simly write this code:
var ips;
var gr= new GlideRecord("cmdb_ci_ip_address");
gr.addQuery("nic.cmdb_ci",record);
gr.query();
while(gr.next()){
ips+=gr.ip_address.toString() + ",";
}
Now ips variable will store all the ips comma seperated as ip1,ip2,ip3 etc
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 01:44 AM
Hello,
Please try below script to achieve your requirement i have pushed it to any array called ipADD and you an assign that array to your string field which will populate IPS in comma seperated values
var ipADD=[];
var gr= new GlideRecord("cmdb_ci_ip_address");
gr.addQuery("nic.cmdb_ci",record);
gr.query();
while(gr.next()){
ipADD.push(gr.ip_address.toString());//this is giving the first value once, second value twice and third one thrice.
}
Please mark my answer correct if it helped you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 02:13 AM
Hi
Thanks for your reply.
But when I print the ipADD it is giving a lot of ips and which I am unable to scroll in the BG script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2022 02:17 AM
Hi Madhu,
I believe its printing repeated value because record value try to use for in loop so it will execute for each value in record 1 by 1 or you can use indexof function to maintain unique Array as below
For in Loop
var arr=['b4fd7c8437201000deeabfc8bcbe5dc1','a9a2d111c611227601fb37542399caa8']; // for ex
var ip;
var ipArr=[];
for(record in arr) {
var gr= new GlideRecord("cmdb_ci_ip_address");
gr.addQuery("nic.cmdb_ci",arr[record]);
gr.query();
while(gr.next()){
ip=gr.ip_address.toString();
ipArr.push(ip.toString());//this is giving the first value once, second value twice and third one thrice.
}
}
gs.print("Check : " + ipArr);
using indexof
var uniqueipArr =[];
for ( ipadd in ipArr){
if (uniqueipArr.indexOf(ipArr[ipadd]) == -1){
uniqueipArr.push(ipArr[ipadd]);
}
}
//gs.print("test After indexof use : " + uniqueipArr)