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 @Tai Vu in my case i have a custom filed in discover model (discovery_model.u_software_name), so first i need to filter based discovery_model.u_software_name in software installation table, and then i need to groupby installed on at that point there will be count for each discovery model ryt. . That records count  i need to store in my custom table field used count. The matching is done between tables by the discovery_model.u_software_name in the software installation and u_name field in custom table.

@Tai Vu  its not ,considering grouby installed on....every used count is updating as 1

var count = new GlideAggregate('cmdb_sam_sw_install');
count.groupBy('discovery_model.u_software_name');
count.groupBy('installed_on');
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);
    }
}

Ravi Gaurav
Giga Sage

Hi @Sharath807 

 

You can update the script to the below :-

 

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()) {
var groupCount = parseInt(count.getAggregate("COUNT"), 10); 
totalCount += groupCount; // Add this count to the total
gs.info(groupCount + "--" + count.getValue("installed_on"));
}

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

Hi @Ravi Gaurav not working, displaying actual count.  I need to display groupby count 

Try the below :-

var totalCount = 0;
var count = new GlideAggregate('cmdb_sam_sw_install');
count.addEncodedQuery("discovery_model.u_software_name=c421a3e387e4d21075920d460cbb350b");
count.groupBy('installed_on'); // Grouping by 'installed_on'
count.query();

while (count.next()) {
totalCount++; // Increment the total count for each group
gs.info("Installed on: " + count.getValue('installed_on')); // Display the value of 'installed_on' for each group
}

gs.info("Total group count: " + totalCount); // Display the total number of groups