Populate the array to a string field

Madhu5
Tera Contributor

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];
}

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

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

 

View solution in original post

4 REPLIES 4

SumanthDosapati
Mega Sage
Mega Sage

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

Mohith Devatte
Tera Sage
Tera Sage

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

 

Hi @Mohith Devatte ,

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.

Chetan Mahajan
Kilo Sage
Kilo Sage

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)