- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 03:38 AM
Hi,
This may be a newbie script question but this is annoying for me.
I wrote the script below in the reference qualifier of a group field, but it doesn't work.
-----------------
javascript:
var gr2 = new GlideRecord("sys_user_group");
gr2.addQuery("type", "CONTAINS", "791a35c11b68dd1088c1553b234bcb55");
gr2.query();
var arr = [] ;
var i = 0 ;
while (gr2.next()) {
arr.push(gr2.sys_id) ;
gs.addInfoMessage(i-1 + " : " + arr[i-1]);
gs.addInfoMessage(i + " : " + arr[i]);
i ++;
}
gs.addInfoMessage("afterpush 0 :" + arr[0]);
gs.addInfoMessage("afterpush 1 :" + arr[1]);
"sys_idIN" + arr ;
-----------------
and the results of the messages are below;
-----------------
-----------------
I don't get even though "arr.push" is executed twice but the single sysid is in index 0 and 1.
I guess "push" should add new values in an array...
Please tell me how I should write.
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 04:16 AM
Hi John,
This is expected behavior and logically correct. When you push GlideRecord referenced data into array it passes stores the reference of the object into the array instead of the value you are passing so the idea is to store the value that can be achieved by explicitly converting the value to toString before storing into the array
arr.push(gr2.sys_id.toString()) ;
or
use Servicenow given methods which is the preferred way of retrieving value as a string.
arr.push(gr2.getValue("sys_id")) ;
KB Article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0858268
Read more on this topic here Pass by Value vs Pass by reference
Regards,
Muhammad
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 04:13 AM
array.push expects a string value to be stored if we want to push in ',' separated value.
gr.sys_id does not return the data in string format, either you need to convert that in string or use getUniqueValue option.
array.push is something a javascript thing which works this way strangely.
please mark the answer correct if it worked or helped
thanks
Harshad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 05:47 PM
Hello Harshad, and thanks to your explanation. I got it 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 04:16 AM
Hi John,
This is expected behavior and logically correct. When you push GlideRecord referenced data into array it passes stores the reference of the object into the array instead of the value you are passing so the idea is to store the value that can be achieved by explicitly converting the value to toString before storing into the array
arr.push(gr2.sys_id.toString()) ;
or
use Servicenow given methods which is the preferred way of retrieving value as a string.
arr.push(gr2.getValue("sys_id")) ;
KB Article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0858268
Read more on this topic here Pass by Value vs Pass by reference
Regards,
Muhammad
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2022 05:51 PM
Hi Muhammad,
Your explanation is deeply understandable for me because the KB article is shared as a reason.
Thank you!