Script on reference qualifier using an array.push() go wrong

John91
Tera Contributor

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;

-----------------

-1 : undefined
0 : 3a1e054b1bc0d9103967217b234bcb2f
0 : 730ec54b1bc0d9103967217b234bcba7
1 : 730ec54b1bc0d9103967217b234bcba7
afterpush 0 :730ec54b1bc0d9103967217b234bcba7
afterpush 1 :730ec54b1bc0d9103967217b234bcba7

-----------------

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

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

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

Regards,
Muhammad

View solution in original post

8 REPLIES 8

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

Hello Harshad, and thanks to your explanation. I got it 🙂

MrMuhammad
Giga Sage

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

Regards,
Muhammad

Hi Muhammad,

Your explanation is deeply understandable for me because the KB article is shared as a reason.

Thank you!