setLimit(3) returning 4 records why?

Sameer Shinde
Tera Contributor

Hello Everyone,

I was trying to solve below problem statement:

1. Get the count of incident group by caller and print only 3 caller having maximum number of incidents.

 

I tried below Script:

 

var incidentGR = new GlideAggregate('incident');
incidentGR.groupBy('caller_id');
incidentGR.addAggregate('COUNT', 'number');
incidentGR.orderByAggregate('COUNT', 'number');
incidentGR.setLimit(3);
incidentGR.query();
while(incidentGR.next()) {
    gs.info("Caller Name:"+incidentGR.getDisplayValue('caller_id'));
    gs.info("Count:"+incidentGR.getAggregate('COUNT', 'number'));
}
 
though setLimit(3) is defined it is returning me 4 rows instead of 3.
 
NOTE: I'm running this script in background and SnUtils extension is enabled on chrome.
 
Can anyone please explain me issue?
 
Thanks in advance,
SameersetLimit error.jpg
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Sameer,

The setLimit method does not work with GlideAggregate, so it's likely that this line is being ignored, and you would get the same results if it were excluded.  You can either wrap this GA in a for loop:

for(var i=0; i<3;i++) {

 or replace your setLimit line with:

incidentGR.chooseWindow(1,3);

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/g... 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Hi Sameer,

The setLimit method does not work with GlideAggregate, so it's likely that this line is being ignored, and you would get the same results if it were excluded.  You can either wrap this GA in a for loop:

for(var i=0; i<3;i++) {

 or replace your setLimit line with:

incidentGR.chooseWindow(1,3);

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/g... 

incidentGR.chooseWindow(1,3); works fine.

 

Thank you!