Solved: issue in counting incident category wise - ServiceNow Community

issue in counting incident category wise

Chandan Kumar1
Giga Contributor

I want to print the incident count category-wise. please help

for exp:

Category: : Database || No Of incidents => 4

Category:  Hardware || No Of incidents => 10

1 ACCEPTED SOLUTION

Voona Rohila
Kilo Patron
Kilo Patron

Hi Chandan

Try this code in your background script.

//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
//count.addQuery('active', '=','true');
count.addAggregate('COUNT', 'category');
count.query();  
while (count.next()) {
   var category = count.category;
   var categoryCount = count.getAggregate('COUNT', 'category');
   gs.info("There are currently " + categoryCount + " incidents with a category of " + category);
}

Check below link : https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideAggregate...


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

4 REPLIES 4

Voona Rohila
Kilo Patron
Kilo Patron

Hi Chandan

Try this code in your background script.

//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
//count.addQuery('active', '=','true');
count.addAggregate('COUNT', 'category');
count.query();  
while (count.next()) {
   var category = count.category;
   var categoryCount = count.getAggregate('COUNT', 'category');
   gs.info("There are currently " + categoryCount + " incidents with a category of " + category);
}

Check below link : https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideAggregate...


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thanks it is working now

Glad to know that my response helped. Please mark response helpful as well.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

ShreyasG
Tera Contributor

Hi Chandan,

 

Recently i had an experience with this code and i was asked to do it without using glide Aggregate, Reason being that it will take lot of time in real time instance where the count is quite high. I could not do it in the interview. So after several tries here is the code that i have found for doing it without Glide Aggregate.

var incidentGr = new GlideRecord('incident'); //creating an object named incidentGr
incidentGr.query();

//gs.log(JSON.stringify(incidentGr,null,2));
var categoryCount = {}; //creating Empty Object

while (incidentGr.next()) { // iterte over the properties of incidentGR
    var category = incidentGr.category + "";

    if (category == "") {
        category = 'not mentioned';
    }

    if (categoryCount[category]) { // checks if the category exists in the object categoryCount
        categoryCount[category]++; // it will store the count dinamially no need of explicit variable count js object functionallity
    } else {
        categoryCount[category] = 1; // if it is coming for first time we need to initialaize it
    }
    // categoryCount{inquiry: 2,database : 1}
}

gs.log(JSON.stringify(categoryCount, null, 2));

Output: 

*** Script: {
  "inquiry": 33,
  "Hardware": 1,
  "database": 2,
  "software": 13,
  "hardware": 9,
  "network": 5,
  "not mentioned": 4
}