Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

show the list of email address of all the user

Anjali yadav1
Tera Contributor
var grUser=new GlideRecord('sys_user');
grUser.query();

while(grUser.next())
{
gs.print(grUser.email);
}
 
i run it in background script of PDI but on running i got not authorized as output pls help me to corrected error
5 REPLIES 5

Bhavya11
Kilo Patron
Kilo Patron

Hi @Anjali yadav1 

 

you script is correct only.

not authorised - its due the session issue so please re-login or reload the page and execute the script it will work

 

to make more accurate add few query in your script like below

 

var grUser=new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.query();

while(grUser.next())
{
gs.print(grUser.email);
}

 

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

 

Thanks,

BK

 

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @Anjali yadav1  check below code

var grUser = new GlideRecord('sys_user');
grUser.query();

while (grUser.next()) {
gs.print(grUser.email);
}
Screenshot 2024-07-15 at 10.27.03 AM.png

Kieran Anson
Kilo Patron

Additionally, use getValue rather than passing grUser.email which is GlideElement. You'll run into pass-by-reference issues if you use this logic to push glide record values to an array or other data type.

Avoiding Pass-By-Reference Using getValue() & setValue() - ServiceNow Developer Pro-Tips (snprotips....

 

var grUser = new GlideRecord('sys_user');
grUser.query();

while (grUser.next()) {
    gs.print(grUser.getValue('email'));
    }

Amit Pandey
Kilo Sage

Hi @Anjali yadav1 

 

Your script is correct! I ran the same in BS and got the results. See screenshot-

 

AmitPandey_0-1721021883595.png

 

Regards,

Amit