GlideAggregate Query

madhusudanshett
Kilo Contributor

Hello Everyone

I am using the following code to display CI and their count from Incident table.

var count = new GlideAggregate('incident');

count.addNotNullQuery('cmdb_ci');

count.addAggregate('COUNT', 'cmdb_ci');

count.orderByAggregate('COUNT', 'cmdb_ci');

//count.setLimit(limit);

count.query();

while (count.next()) {

var ci = count.cmdb_ci;

var incCount = count.getAggregate('COUNT', 'cmdb_ci');

gs.log("CI: "+ci.getDisplayValue());

gs.log("Count: "+incCount);

}

It gives me the correct output. But I want to display only first 5 CI and their count.

I think setLimit doesnt work here.

ChrisB

b-rad

ctomasi

Please help me on this issue.

Thanks in advance.

1 ACCEPTED SOLUTION

akashsrm100
Kilo Guru

for(var i=0; i<5;i++)


{


if(count.next()) {


var ci = count.cmdb_ci;


var incCount = count.getAggregate('COUNT', 'cmdb_ci');


gs.log("CI: "+ci.getDisplayValue());


gs.log("Count: "+incCount);


}


}


Hope this can work


Ps hit like helpful If helps/correct


View solution in original post

7 REPLIES 7

Midhun1
Giga Guru

Hi Madhusudan,



Instead count.setLimit(5) use count.chooseWindow(1,5);


I think setLimit won't work for glide Aggregate just now i tried i got the solution with chooseWindow(first,last).



For more info:


The chooseWindow(first,last) method lets you set the first and last row number that you want to retrieve and is typical for chunking-type operations. The rows for any given query result are numbered 0..(n-1), where there are n rows. The first parameter is the row number of the first result you'll get. The second parameter is the number of the row after the last row to be returned. In the example below, the parameters (10, 20) will cause 10 rows to be returned: rows 10..19, inclusive.


Hi Midhun



Thank you very much for the help.


Pastupe
Mega Guru

Thank you, this has resolved also my issue, loop has helped me to learn how to set limit to max results to be returned from database using aggregate



/Petr