Write a script to print Incident count based on priority.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 02:33 AM
Below is my script which is not giving expected O/P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 03:08 AM
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:
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]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 03:19 AM
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 03:36 AM
what's your business requirement?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 03:48 AM
Hi @Parth_S check below code its working fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 05:35 AM
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:
-------------------------------------------------------------------------
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
-------------------------------------------------------------------------