How do i add an array to a field ,separated by comma

Chinmayee1
Giga Contributor

Hi,

I need to add an array of values  to a field separated by , comma.i m getting only one value in that but not all the values.

for example array = {1,2,3}

field A should show me 1,2,3 but it shows me only 2 

if i assign the same to a variable and check in the logs , it shows me 1,2,3 .but in the field only 2.

Can anyone please advise ?

19 REPLIES 19

SaiRaviKiran Ak
Giga Guru

Try Below Code:

 

(function executeRule(current, previous /*null when async*/) {

var hname = [];
var tst ="";
var i;

var grhost = new GlideRecord('u_hostname');
grhost.addQuery('u_justification',current.number);
grhost.query();
while(grhost.next()){

hname.push(grhost.u_hostname.toString());
//current.u_hostname.push(grhost.u_hostname.toString());

}
for( i=0; i<hname.length; i++){


tst = tst +hname[i] + ",";

}
gs.log('tst :'+tst);

current.u_hostname = hname.join(',');
gs.log('hname :'+hname);
gs.log('current.hostname :'+current.u_hostname);

 

})(current, previous);

No, Its the same .

Try Below Code and let me know the info message values:

 

(function executeRule(current, previous /*null when async*/) {

var hname = [];
var value;
var grhost = new GlideRecord('u_hostname');
grhost.addQuery('u_justification',current.number);
grhost.query();
while(grhost.next()){
gs.addInfoMessage("grhost name"+grhost.u_hostname);
hname.push(grhost.u_hostname.toString()+'');
gs.addInfoMessage("hname"+hname);
value = hname.join(',');
}

gs.addInfoMessage("value "+value);
current.u_hostname =value;
gs.addInfoMessage("hostname"+u_hostname);
current.update();

})(current, previous);

but.. why use .join(',') when hname is already an array. .join() is used to convert a string into an array.

Hi,

Did you solve this problem? I am having similar issue?

Thanks,

Debashree