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.

Query using join, having, count, etc

MesihasAle
Tera Contributor

I want to create a query : select top 10 users(User table) with more that 1 position (Job table).

I am doing this but seems like is not working, any ideas where this is failing?

 

var grEmployees = new GlideRecord('sys_user');

var grPositions = grEmployees.addJoinQuery('sn_hr_core_job', 'user', 'user');

grPositions.addAggregate('COUNT', 'user');

grPositions.addHaving('COUNT', '>', 1);

grPositions.orderBy('name');

grEmployees.query();

var count = 0;

while (grEmployees.next() && count < 10) {
    gs.info('Employee ID: ' + grEmployees.name + ', Name: ' + grEmployees.email);
    count++;
}

 

0 REPLIES 0