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

Nishant8
Giga Sage
Giga Sage

Hello @timnardoni , can you please try following:

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

Ankur Bawiskar
Tera Patron
Tera Patron

@timnardoni 

did you print the row count?

Try this and skip the query business rule and see what count it gives

var users = new GlideRecord('sys_user');
users.addActiveQuery(); //Active users only
users.setWorkflow(false); // skip query business rule
users.query();
gs.info('row count' + users.getRowCount());
while (users.next()) {
	gs.info(users.user_name);
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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 

Perfect, thank you, @Robert H and @Nishant8

 

@Ankur Bawiskar, that row count trick is a good; I will use it again! 😁