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.

Javascript to pull list of all active users

timnardoni
Tera Contributor

Hello, all. This script is meant to produce a list of active users but skipped ~1,500. What am I doing wrong?

Thank you in advance!

-Tim

 

var users = new GlideRecord('sys_user');
users.addQuery('active', true); //Active users only
users.query();
    
while (users.next()) {
        users.next();
	gs.info(users.user_name);
    }

 

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @timnardoni ,

 

You're using the "next()" method twice per loop. That's why every second user is skipped.

Please remove the second one, so that it looks like this:

 

var users = new GlideRecord('sys_user');
users.addQuery('active', true); //Active users only
users.query();
    
while (users.next()) {
    gs.info(users.user_name);
}

 

Regards,

Robert 

View solution in original post

5 REPLIES 5

Hello @timnardoni ,

 

Thanks for the feedback.

One quick note: please do not use getRowCount in production code, as it can cause performance issues. See here for a discussion and a better alternative.

 

Regards,

Robert