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

Viraj Hudlikar
Giga Sage

Hello @Asmita7 

 

Not sure with you usecase but if need code to do so below is sample code:

// Step 1: Get incident counts by priority
    var incidentCounts = {};
    var ga = new GlideAggregate('incident');
    ga.addAggregate('COUNT');
    ga.groupBy('priority');
    ga.query();
    while (ga.next()) {
        var priority = ga.getValue('priority');
        var count = ga.getAggregate('COUNT');
        incidentCounts[priority] = parseInt(count, 10);
    }

    // Step 2: Sort priorities by count in descending order
    var sortedPriorities = Object.keys(incidentCounts).sort(function(a, b) {
        return incidentCounts[b] - incidentCounts[a];
    });

    // Step 3: Print the count of incidents for each priority
    var result = '';
    sortedPriorities.forEach(function(priority) {
        result += 'Priority ' + priority + ' - has ' + incidentCounts[priority] + ' incidents\n';
    });

    // Step 4: Fetch and print incident numbers for the top 3 priorities
    result += '\nTop 3 Priorities with Incident Numbers:\n';
    sortedPriorities.slice(0, 3).forEach(function(priority) {
        result += 'Priority ' + priority + ' - has ' + incidentCounts[priority] + ' incidents\n';
        var gr = new GlideRecord('incident');
        gr.addQuery('priority', priority);
        gr.query();
        while (gr.next()) {
            result += 'Incident Number: ' + gr.getValue('number') + '\n';
        }
        result += '\n';
    });

    // Print the result
    gs.print(result);

I wrote this code in background script and as per your requirement it works.

 

For GlideAggregate usage check this video - https://youtu.be/tr4l4GtevfM?si=7mOxc7xbo5IXppoO


If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar.