GlideAggregate return wrong value

Sharath807
Tera Contributor

Hi all , Iam trying to get the count based on the query , but in system records its showing count as 534.

but when i run script in background its showing as 543. did i miss anything?

var totalCount = 0;
var count = new GlideAggregate('cmdb_sam_sw_install');
count.addEncodedQuery("discovery_model.u_software_name=c421a3e387e4d21075920d460cbb350b");

count.addAggregate('COUNT');

count.groupBy('installed_on');

count.query();
while (count.next()) {

totalCount = totalCount+1;

gs.info(count.getAggregate("COUNT")+ "--"+count.getValue("installed_on"));

}
gs.info("Total count: " + totalCount);

22 REPLIES 22

hi @Ravi Gaurav  the count is still different
Screenshot (61).pngScreenshot (62).png

then please tell the filter it mean that some fishy in filter, Can you go to the list view and apply the filter and copy the query and paste it here

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

@Ravi Gaurav 

this is filter user  :  discovery_model.u_software_name=c421a3e387e4d21075920d460cbb350b

next is group by installed on

Hi @Ravi Gaurav 

.. now i tried to get the count dynamic to all records, but its showing counts only based on the filter ( discovery_model.u_software_name)   not considering goupby (installed on.)

var count = new GlideAggregate('cmdb_sam_sw_install');
count.groupBy('discovery_model.u_software_name');
count.addAggregate('COUNT');
count.query();


while (count.next()) {
    var softwareName = count.getDisplayValue('discovery_model.u_software_name');
    var totalCount = count.getAggregate('COUNT');  
   
    gs.info("Software: " + softwareName + ", Installations count: " + totalCount);
   
   
    var softwareNormGR = new GlideRecord('u_norm_software');
    softwareNormGR.addQuery('u_name', softwareName);  
    softwareNormGR.query();

   
    if (softwareNormGR.next()) {
        softwareNormGR.u_used_count = totalCount;  
        softwareNormGR.update();  
        gs.info("Updated u_norm_software record for " + softwareName + " with total installations: " + totalCount);
    } else {
        gs.error("No matching u_norm_software record found for the software name: " + softwareName);
    }
}



GeenlakuntaM
Tera Contributor
Hi @Sharath807 , Try the below code:
 
var TotalRecords = 0;
var count = new GlideAggregate('cmdb_sam_sw_install');
count.addEncodedQuery("discovery_model.u_software_name=c421a3e387e4d21075920d460cbb350b");
count.groupBy('installed_on');
count.addAggregate('COUNT');
count.query();
while (count.next()) {
TotalRecords = count.getAggregate("COUNT");
}
gs.info("Total number of records: " + TotalRecords);
 
 
 
Please mark my answer helpful if the issue is resolved.