The CreatorCon Call for Content is officially open! Get started here.

Write a script to print Incident count based on priority.

Parth_S
Tera Contributor

Below is my script which is not giving expected O/P.

 

var gr = new GlideAggregate('incident');
gr.groupBy('priority');
gr.query();
var count = 0;
while(gr.next()){
    gs.info(gr.priority);
    count = count + 1;
    gs.info('Count is ' + count);
}
10 REPLIES 10

Nishant8
Giga Sage
Giga Sage

Hello @Parth_S, you seem to mixing up GlideRecord and GlideAggregate; GlideAggregate counts the record without looping thru. you can try something below if trying to count the priority:

 

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT','priority');
agg.query();
while(agg.next()){
var priorityCount = agg.getAggregate('COUNT','priority');
var priority = agg.getDisplayValue('priority');
gs.info('Display the count {0} for {1}', [priorityCount,priority]);
}

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @Parth_S 

Here is the updated script:

var gr = new GlideAggregate('incident');
gr.groupBy('priority'); // Group incidents by priority
gr.addAggregate('COUNT'); // Add a count aggregate
gr.query();

while (gr.next()) {
    var priority = gr.getValue('priority'); // Retrieve the priority value
    var count = gr.getAggregate('COUNT'); // Get the count for this priority group
    gs.info('Priority: ' + priority + ', Count: ' + count);
}

Result:

JuhiPoddar_0-1735557526986.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily in community!"

 

Thank You
Juhi Poddar

Ankur Bawiskar
Tera Patron
Tera Patron

@Parth_S 

what's your business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @Parth_S  check below code its working fine 

var inc = new GlideAggregate('incident');
inc.groupBy('priority');
inc.addAggregate('COUNT');
inc.query();

gs.print('Incident count by priority:');
while (inc.next()) {
var priority = inc.getValue('priority');
var count = inc.getAggregate('COUNT');
gs.print('Priority: ' + (priority || 'None') + ', Count: ' + count);
}
Screenshot 2024-12-30 at 5.17.35 PM.png
if my answer helps you mark helpful and accept solution 

Runjay Patel
Giga Sage

Hi @Parth_S ,

 

You can use below script to get the count of incident based on Priority.

var gr = new GlideAggregate('incident');
gr.groupBy('priority');
gr.query();
var count = 0;

while (gr.next()) {
    var priority = gr.getValue('priority'); // Use getValue() to access grouped field
    gs.info('Priority: ' + priority);
    count = count + 1;
    gs.info('Count is: ' + count);
}

 

Output:

RunjayPatel_0-1735565695400.png

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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