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

Please add list filter on incident table i.e., "create on last 12 months" and copy that query and instead of previous one past the new one. And also compare the field backend names present in the script with names present in your instance. It might work for you because I have just run the same background script in my instance and it is working with all the required details.

Thank You!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Community Alums
Not applicable

Hi @abhisek  ,

Please try now 

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);

}

 

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

Thanks and Regards 

Sarthak

Hi @Community Alums 

It is not populating the usernames who created the max and min no of incidents along with the incident number.

Thanks&Regards,

Abhisek Chattaraj.

A report does.


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

thullurishalini
Kilo Guru

Hi @abhisek  

To achieve this in ServiceNow, you can use the following steps:

  1. Create a Report:

    • Go to Reports > Create New.
    • Select the Table as Incident.
    • Choose the Type of report, such as Bar Chart or List.
  2. Group by Created By:

    • In the Group By field, select Created By.
    • This will group incidents by the user who created them.
  3. Add Aggregation:

    • Add an Aggregation to count the number of incidents for each user.
    • Use the Count function on the Number field.
  4. Sort the Results:

    • Sort the results in descending order to find the user with the maximum number of incidents.
    • Sort in ascending order to find the user with the minimum number of incidents.
  5. Filter the Results:

    • Apply filters if needed to narrow down the data to a specific time range or other criteria.
  6. Save and Run the Report:

    • Save the report and run it to see the results.

This will give you a clear view of which user created the most and the least number of incidents, along with the incident numbers.

Thanks ,

Shalini