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 @Anurag Tripathi 

It is just background script. 

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

What do you mean by minimum? -> 1

What about records created on behalf of others? -> no need to check it.

If you need to see it all, then why don't you just open the list view of incident.

Change the list view for yourself and only include the number and caller.

Order by Caller. 

This is your list, maybe export in excel.

 

-Anurag

I want to check it through background script.

Try this then

 

var records = new GlideRecord('incident');
records.orderBy('caller_id');
records.query();
while (records.next()) {
	gs.print('Number = ' + records.number + ' : Caller = ' + records.caller_id.getDisplayValue());
}

 

-Anurag

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