Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Group by Priority

NBK
Tera Contributor

How to know the count of incidents in each priority i.e., in priority - 1 or 2 or 3 or 4 or 5 separately by using background script.

3 ACCEPTED SOLUTIONS

Hajar BENJAHHAR
Mega Sage

Hello @NBK , 

 

You can use this script to get the count of incidents per priority : 

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'priority'); 
agg.orderBy('priority'); 
agg.query(); 
while (agg.next()) { 
  var priority = agg.priority.getDisplayValue();
  var count = agg.getAggregate('count', 'priority');
  var agg2 = new GlideAggregate('incident');   
  agg2.addAggregate('count', 'priority');
  agg2.orderBy('priority');
  gs.info(priority + ": number of incidents:" + count);
}

 

I hope you find this helpful!

 

Best regards, 

Hajar

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@NBK Here is the script.

 

var incidentPriority = new GlideAggregate('incident');
incidentPriority.addAggregate('COUNT');
incidentPriority.groupBy('priority');
incidentPriority.query();
while(incidentPriority.next()){
    gs.info(incidentPriority.getDisplayValue('priority')+'('+incidentPriority.getAggregate('COUNT')+')');
}

 

Here is how the output looks

Screenshot 2024-08-30 at 12.31.30 AM.png

Here is how it looks when group by in the list.

Screenshot 2024-08-30 at 12.31.34 AM.png

Please mark the response helpful and accepted solution if it manages to answer your question.

View solution in original post

_Tushar Soni
Kilo Sage
Kilo Sage

Hello @NBK ,

// Create a GlideAggregate object for the incident table
var inc = new GlideAggregate('incident');

// Add a group by clause for the priority field
inc.addAggregate('COUNT', 'priority');

// Group by priority
inc.groupBy('priority');

// Query the records
inc.query();

// Iterate through the result
while (inc.next()) {
    var priority = inc.getValue('priority');
    var count = inc.getAggregate('COUNT', 'priority');
    
    gs.info('Priority ' + priority + ': ' + count + ' incidents');
}

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!

View solution in original post

3 REPLIES 3

Hajar BENJAHHAR
Mega Sage

Hello @NBK , 

 

You can use this script to get the count of incidents per priority : 

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'priority'); 
agg.orderBy('priority'); 
agg.query(); 
while (agg.next()) { 
  var priority = agg.priority.getDisplayValue();
  var count = agg.getAggregate('count', 'priority');
  var agg2 = new GlideAggregate('incident');   
  agg2.addAggregate('count', 'priority');
  agg2.orderBy('priority');
  gs.info(priority + ": number of incidents:" + count);
}

 

I hope you find this helpful!

 

Best regards, 

Hajar

Sandeep Rajput
Tera Patron
Tera Patron

@NBK Here is the script.

 

var incidentPriority = new GlideAggregate('incident');
incidentPriority.addAggregate('COUNT');
incidentPriority.groupBy('priority');
incidentPriority.query();
while(incidentPriority.next()){
    gs.info(incidentPriority.getDisplayValue('priority')+'('+incidentPriority.getAggregate('COUNT')+')');
}

 

Here is how the output looks

Screenshot 2024-08-30 at 12.31.30 AM.png

Here is how it looks when group by in the list.

Screenshot 2024-08-30 at 12.31.34 AM.png

Please mark the response helpful and accepted solution if it manages to answer your question.

_Tushar Soni
Kilo Sage
Kilo Sage

Hello @NBK ,

// Create a GlideAggregate object for the incident table
var inc = new GlideAggregate('incident');

// Add a group by clause for the priority field
inc.addAggregate('COUNT', 'priority');

// Group by priority
inc.groupBy('priority');

// Query the records
inc.query();

// Iterate through the result
while (inc.next()) {
    var priority = inc.getValue('priority');
    var count = inc.getAggregate('COUNT', 'priority');
    
    gs.info('Priority ' + priority + ': ' + count + ' incidents');
}

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!