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.

Array values are not getting printed correctly

Kiruthikroshan
Tera Contributor

Hi,

 

I need a help regarding a Background script.

 

I need to fetch and print first 5 users from each Department the User table. Below is the script that I have written.

 

var dep = new GlideAggregate('sys_user');
dep.addNotNullQuery('department');
dep.groupBy('department');
dep.query();
while(dep.next())
{
    var dept = dep.department;
    gs.print("Department is " + dept.getDisplayValue());
        var array = [];
        var user = new GlideRecord('sys_user');
        user.addQuery('department',dept);
        user.orderBy('name');
        gr.setLimit( 5 );
        user.query();
        while(user.next())
        {
            array.push(user.name);
        }
        gs.print(array);    
}
 
After running the script, it gives output as attached. Please help me to find what is wrong with my script.
Thanks,
Kiruthikroshan M
0 REPLIES 0