Help with Script "slice()"

sunitac
Kilo Contributor

Script:

```

var gm= new GlideRecord("sys_user_grmember");

gm.addQuery("group","220f8e71c61122840197e57c33464f70");

gm.query();

var list='';

while(gm.next())

    list += gm.user.toString() + " , " ;

list = list.slice(0, - 1);

gs.log(list);

```

Prints the trailing comma as well, even though I have got slice().

But below script removes the last charactor:

```

var abc = "123456789";

abc = abc.slice(0, - 1);

gs.log(abc);

```

Prints: 12345678

Can someone advise why first script is not removing the last comma?

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Sunita,



Try with the updated code below and let me know.




var gm= new GlideRecord("sys_user_grmember");


gm.addQuery("group","220f8e71c61122840197e57c33464f70");


gm.query();


var list='';


if(gm.next()) {


list += gm.user.toString() ;




}


while(gm.next())


list += "," +gm.user.toString() ;


gs.log(list);










Thanks.



I know that will work, I have it working. Basic logic.



Question was: in my code, why slice() is not working for case#1.


Any Thoughts?