GlideAggregate setLimit(5) returning more than 5 results in background script.

AbW
Mega Contributor

Please ignore the business use case.
The focus is only on understanding the GlideAggregate behavior below.
I am learning GlideAggregate and want to understand why this is happening.

I am running the following script in Background Scripts (sys.scripts):

 

// You’re building a dashboard widget for top 5 assignment groups with most active tasks. How can you limit the result?

var result = new  GlideAggregate('task');
result.addQuery('active', true);
result.groupBy('assignment_group');
result.addAggregate('COUNT', 'assignment_group');

result.orderByAggregate('COUNT','assignment_group');
result.setLimit(5);
result.addNotNullQuery('assignment_group');
result.query();

while(result.next()){
    gs.info(result.getDisplayValue('assignment_group') + ': ' + result.getAggregate('COUNT' , 'assignment_group' ));
}

 

Even though setLimit(5) is used, the script prints 6 rows.
There are definitely more than 6 groups in the data, so I expect the output to be limited to 5 rows only.

Can someone explain:

  • Why setLimit(5) is not limiting the result to 5 rows

  • How setLimit() behaves when used with GlideAggregate, groupBy, and orderByAggregate

Any explanation focused on the code behavior would be appreciated.

0 REPLIES 0