incident

abhisek
Tera Contributor

I need to populate the username who created the max no of incidents and min no of incidents along with the incident number.

Can anyone please let me know how I can achieve it?

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hi @abhisek  ,

 

Here is the background script for your requirement.

 

(function() {
    var incidentGr = new GlideAggregate('incident');
    incidentGr.addAggregate('COUNT', 'sys_created_by');
    incidentGr.groupBy('sys_created_by');
    incidentGr.query();

    var maxIncidents = 0;
    var minIncidents = Infinity;
    var maxUser = '';
    var minUser = '';

    while (incidentGr.next()) {
        var count = parseInt(incidentGr.getAggregate('COUNT', 'sys_created_by'), 10);
        var user = incidentGr.sys_created_by.toString();

        if (count > maxIncidents) {
            maxIncidents = count;
            maxUser = user;
        }

        if (count < minIncidents) {
            minIncidents = count;
            minUser = user;
        }
    }

    var maxUserIncidents = [];
    var minUserIncidents = [];

    if (maxUser) {
        var maxIncidentGr = new GlideRecord('incident');
        maxIncidentGr.addQuery('sys_created_by', maxUser);
        maxIncidentGr.query();
        while (maxIncidentGr.next()) {
            maxUserIncidents.push(maxIncidentGr.number.toString());
        }
    }

    if (minUser) {
        var minIncidentGr = new GlideRecord('incident');
        minIncidentGr.addQuery('sys_created_by', minUser);
        minIncidentGr.query();
        while (minIncidentGr.next()) {
            minUserIncidents.push(minIncidentGr.number.toString());
        }
    }

    gs.print('User with max incidents: ' + maxUser + ' (Count: ' + maxIncidents + ')');
    gs.print('Incidents: ' + maxUserIncidents.join(', '));
    gs.print('User with min incidents: ' + minUser + ' (Count: ' + minIncidents + ')');
    gs.print('Incidents: ' + minUserIncidents.join(', '));
})();

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

19 REPLIES 19

Hi @Amitoj Wadhera 

Thanks for your reply.

Could you please let me know why function() declaration is needed here at the beginning of the script ?

Because best practice is to place your script within a function.

On topic: I fully agree with Anurag: it makes no sense to do this via a back ground script, if you can easily just get the incidents, do a group by 'opened_by' and sort by who created the most. 
If you create it as report, you will have it there always, instead of needing to look for the script.
And least amount of incidents? Create a report (yes: reports are way easier than scripts) and do a related list condition on 'incidents' with 0. You will see all users that never created an incident.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Community Alums
Not applicable

Hi @abhisek  ,

Please check below script 

var ga= new GlideAggregate('incident');
ga.addEncodedQuery("sys_created_onONLast 12 months@javascript&colon;gs.beginningOfLast12Months()@javascript&colon;gs.endOfLast12Months()");
ga.addAggregate('COUNT', 'caller_id');
ga.orderByAggregate('COUNT', 'caller_id');
ga.query();
while (ga.next()) {
         var count = ga.getAggregate('COUNT', 'caller_id');
	gs.info(ga.caller_id.getDisplayValue() + ' - ' + count);
}

Hi @Anurag Tripathi , I think by mistake you write GlideRecord, you mean GlideAggregate correct ? 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

@Community Alums Thanks mate, corrected it.

 

-Anurag

Hi @Community Alums 

This script is not populating the username along with the incident number.