- 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:32 AM
Thanks for your reply.
Could you please let me know why function() declaration is needed here at the beginning of the script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 05:56 AM
Because best practice is to place your script within a function.
On topic: I fully agree with Anurag: it makes no sense to do this via a back ground script, if you can easily just get the incidents, do a group by 'opened_by' and sort by who created the most.
If you create it as report, you will have it there always, instead of needing to look for the script.
And least amount of incidents? Create a report (yes: reports are way easier than scripts) and do a related list condition on 'incidents' with 0. You will see all users that never created an incident.
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
‎05-28-2024 05:34 AM
Hi @abhisek ,
Please check below script
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);
}
Hi @Anurag Tripathi , I think by mistake you write GlideRecord, you mean GlideAggregate correct ?
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-28-2024 05:44 AM
@Community Alums Thanks mate, corrected it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 05:53 AM
Hi @Community Alums
This script is not populating the username along with the incident number.