- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 03:28 AM - edited ‎05-28-2024 03:29 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 03:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:00 AM - edited ‎05-28-2024 06:02 AM
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!!
Thank you!!
Dnyaneshwaree Satpute
Tera Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 06:55 AM
Hi @abhisek ,
Please try now
var ga = new GlideAggregate('incident');
ga.addEncodedQuery("sys_created_onONLast 12 months@javascript:gs.beginningOfLast12Months()@javascript: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2024 03:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2024 04:57 AM
A report does.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 05:22 AM
Hi @abhisek
To achieve this in ServiceNow, you can use the following steps:
Create a Report:
- Go to Reports > Create New.
- Select the Table as Incident.
- Choose the Type of report, such as Bar Chart or List.
Group by Created By:
- In the Group By field, select Created By.
- This will group incidents by the user who created them.
Add Aggregation:
- Add an Aggregation to count the number of incidents for each user.
- Use the Count function on the Number field.
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.
Filter the Results:
- Apply filters if needed to narrow down the data to a specific time range or other criteria.
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