← Script library

Background Scripts

Count Unassigned Active Incidents by Group

Report active incidents with no assignee, grouped by assignment group, without updating any records. Includes incidents with no assignment group.

JavaScript
(function reportUnassignedIncidents() {
  var prefix = '[SN-Tricks:UnassignedIncidents]';
  try {
    var counts = new GlideAggregate('incident');
    counts.addQuery('active', true);
    counts.addNullQuery('assigned_to');
    counts.addAggregate('COUNT');
    counts.groupBy('assignment_group');
    counts.query();

    var total = 0;
    var groups = 0;
    while (counts.next()) {
      var count = parseInt(counts.getAggregate('COUNT'), 10);
      var groupId = counts.getValue('assignment_group');
      var label = groupId
        ? (counts.getDisplayValue('assignment_group') || '(unresolved group)')
        : '(no assignment group)';
      gs.info(prefix + ' ' + label + ' [' + (groupId || 'empty') + ']: ' + count);
      total += count;
      groups++;
    }
    gs.info(prefix + ' Total active incidents without an assignee: ' + total +
      '; assignment-group buckets: ' + groups);
  } catch (error) {
    gs.error(prefix + ' Report failed; any preceding output may be partial: ' + error);
  }
})();

How to use it

1. Open System Definition > Scripts - Background as an authorized administrator in a non-production instance. Select Global scope and paste the script. It reports data only; it does not insert, update, or delete records. 2. Test with active incidents that have an assignee, active incidents without an assignee in two different groups, an active incident with neither group nor assignee, and an inactive incident without an assignee. Only active incidents without an assignee should be counted. 3. Run the script and inspect the output or system logs for [SN-Tricks:UnassignedIncidents]. Each line contains the group display name, sys_id, and count. The empty-group bucket is included. The last line reports the total; no matching incidents produces a total of zero and zero buckets. 4. Compare counts with an incident list filtered to Active is true and Assigned to is empty in the same domain context. Group sys_ids distinguish groups with identical names. The active flag is used instead of hardcoded state values. 5. GlideAggregate performs the count in the database, but large tables can still require an expensive scan. Test runtime before running on production. This administrative background report is not an ACL-filtered end-user report; domain separation and execution context can affect visibility. 6. If an error is logged, disregard partial output and investigate before rerunning. Do not publish logs containing internal group details to unauthorized users. No rollback is required because no records are changed.

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.