Print count and incident numbers for incident with top 3 priorities
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 12:24 AM
Hello friends,
I have a query :
Print count and incident numbers for incident with top 3 priorities
E.g.
Priority 1 - has 50 inc
Priority 3 - has 45 inc
Priority 5 - has 35 inc
Priority 4 - has 5 inc
Priority 2 - has 1 inc
Than i have to print top 3 priority with incident number.
Which approach is better
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 12:34 AM
where are you planning to write this?
We don't know your exact requirement and the business use-case?
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
02-10-2025 05:16 AM
Thank you @Ankur Bawiskar
It will be used in a Scheduled Job or Fix Script .... we need to process these incidents further after filtering.
Please help.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 05:20 AM
so if you are using that in scheduled job then you need to know which query should be applied to perform further processing
the question you asked simply says you want to print
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
02-10-2025 12:42 AM
Hi @Asmita7
The report will be like this
or try like this
// Create a GlideRecord object for the incident table
var gr = new GlideRecord('incident');
// Define an array to hold the top 3 priorities
var topPriorities = [];
// Query the incident table to get the top 3 priorities
gr.orderBy('priority');
gr.setLimit(3);
gr.query();
while (gr.next()) {
// Push the priority into the array if it's not already there
if (topPriorities.indexOf(gr.priority.toString()) === -1) {
topPriorities.push(gr.priority.toString());
}
}
// Now, we will count incidents for each of the top priorities
var priorityCounts = {};
// Initialize counts for each top priority
for (var i = 0; i < topPriorities.length; i++) {
priorityCounts[topPriorities[i]] = 0;
}
// Query incidents again to count them by priority
var countGr = new GlideRecord('incident');
countGr.addQuery('priority', 'IN', topPriorities);
countGr.query();
while (countGr.next()) {
var priority = countGr.priority.toString();
priorityCounts[priority]++;
}
// Print the results
for (var priority in priorityCounts) {
gs.info('Priority: ' + priority + ', Count: ' + priorityCounts[priority]);
}
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************