Print count and incident numbers for incident with top 3 priorities

Asmita7
Tera Expert

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

 

 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Asmita7 

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.

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

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

@Asmita7 

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.

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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Asmita7 

 

The report will be like this

 

AGLearnNGrow_0-1739176720712.png

 

 

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]

****************************************************************************************************************