- 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 03:40 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 03:44 AM - edited ‎05-28-2024 05:43 AM
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'));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 05:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 05:36 AM
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?