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

Anurag Tripathi
Mega Patron
Mega Patron

Populate where?

created the max no of incidents over what period?

Minimum would be 0 for a lot of users, so what needs to happen here

 

Can you elaborate a bit on what you are trying to do?

-Anurag

Here is a sample script to find users what have created max incidents in last 1 year, modify the dates as necessary

 

var records = new GlideAggregate('incident');
records.addQuery('sys_created_on', '>=', gs.beginningOfLastYear());
records.addQuery('sys_created_on', '<=', gs.endOfLastYear());
records.addAggregate('COUNT', 'caller_id');
records.orderByAggregate('COUNT', 'caller_id');
records.query();
while (records.next()) {
	gs.info(records.caller_id.getDisplayValue() + ' - ' + records.getAggregate('COUNT', 'caller_id'));
}

 

-Anurag

Hi @Anurag Tripathi 

Thanks for your reply.

I need to populate the username who created the max no of incidents and min no of incidents since the beginning till the current date along with the incident number.

Hi,

What im struggling to understand is Is this a form you are populating?

Is this a report or just background script?

If a user has a million records created you want to see all the record number?

What do you mean by minimum? is that 0 or 1 ?

What about records created on behalf of others? 

 

Most importantly, what are you trying to achieve by this?

 

-Anurag