- 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 05:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 05:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 05:50 AM
I want to check it through background script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 06:09 AM - edited 05-28-2024 06:09 AM
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());
}
- 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