Incident creation if group does not have members or owners or managers

Kaustubh k
Tera Contributor

Hi @Viraj Hudlikar ,

 

This is in Continuation to the Solution which we discussed in the below link:

 

https://www.servicenow.com/community/incident-management-forum/incident-creation-ig-group-does-not-h...

 

I observed that whenever an existing case is already present in the system but does not have this group in the description, then also the system is not creating a case.

As we are just filtering with the short description.

 

Thanks,

 

 

 

1 ACCEPTED SOLUTION

Hello @Kaustubh k 

Updated one and tested in PDI I had something 15 group without Manager & Members on first run of job it created normal one incident with all group name in description. I updated that existing incident and remove 5 groups name from it and reran the job it created a new incident with only 5 missing group from previous incident created.

var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
    initialize: function() {},

    checkGroups: function() {
        var gr = new GlideRecord('sys_user_group');
        gr.addActiveQuery();
        gr.query();

        var emptyGroups = new Set();

        while (gr.next()) {
            var groupId = gr.sys_id;
            var groupName = gr.name.toString();

            // Check if the group has members
            var memberCount = new GlideAggregate('sys_user_grmember');
            memberCount.addQuery('group', groupId);
            memberCount.addAggregate('COUNT');
            memberCount.query();
            memberCount.next();
            var hasMembers = memberCount.getAggregate('COUNT') > 0;

            // Check if the group has owners or managers
            var hasManager = gr.manager.toString() !== '';

            if (!hasMembers && !hasManager) {
                emptyGroups.add(groupName);
            }
        }

        if (emptyGroups.size > 0) {
            this.createIncident(Array.from(emptyGroups));
        }
    },

    createIncident: function(groupNames) {
        var existingIncident = new GlideRecord('incident');
        existingIncident.addQuery('short_description', 'Groups with no members & managers');
        existingIncident.query();

        var existingGroups = new Set();
        while (existingIncident.next()) {
            var existingDescription = existingIncident.description.toString();
            existingDescription.split('\n').forEach(function(group) {
                existingGroups.add(group.trim());
            });
        }

        var newGroups = groupNames.filter(function(group) {
            return !existingGroups.has(group);
        });

        if (newGroups.length > 0) {
            var inc = new GlideRecord('incident');
            inc.initialize();
            inc.short_description = 'Groups with no members & managers';
            inc.description = 'The following groups have no members or managers:\n' + newGroups.join('\n');
            inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
            inc.insert();
        }
    },

    type: 'CheckEmptyGroups'

};

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

View solution in original post

10 REPLIES 10

@Kaustubh k 

why not simply append the existing incident with this new group?

Consider daily a new group is available with no members it will unnecessarily create those many new incidents daily

If you still want that then update as this

var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
    initialize: function() {},

    checkGroups: function() {
        var gr = new GlideRecord('sys_user_group');
        gr.addActiveQuery();
        gr.query();
        
        var emptyGroups = [];
        
        while (gr.next()) {
            var groupId = gr.sys_id;
            var groupName = gr.name.toString();

            // Check if the group has members
            var memberCount = new GlideAggregate('sys_user_grmember');
            memberCount.addQuery('group', groupId);
            memberCount.addAggregate('COUNT');
            memberCount.query();
            memberCount.next();
            var hasMembers = memberCount.getAggregate('COUNT') > 0;

            // Check if the group has owners or managers
            var hasManager = gr.manager.toString() !== '';

            if (!hasMembers && !hasManager) {
                emptyGroups.add(groupName);
            }
        }
        
        if (emptyGroups.length > 0) {
            this.createOrUpdateIncident(Array.from(emptyGroups));
        }
    },

    createOrUpdateIncident: function(groupNames) {
        // Check if an incident already exists for these groups
        var existingIncident = new GlideRecord('incident');
        existingIncident.addQuery('short_description', 'Groups with no members & managers');
        existingIncident.query();
        
        var existingGroups = new Set();
        if (existingIncident.next()) {
            // Extract existing groups from the incident description
            var existingDescription = existingIncident.description.split('\n');
            for (var i = 1; i < existingDescription.length; i++) {
                existingGroups.add(existingDescription[i]);
            }
        }

        // Find new groups that are not in the existing incident
        var newGroups = groupNames.filter(function(group) {
            return !existingGroups.has(group);
        });

        if (newGroups.length > 0) {
            if (existingIncident.isValidRecord()) {
                // Update the existing incident with new groups
                existingIncident.description += '\n' + newGroups.join('\n');
                existingIncident.update();
            } else {
                // Create a new incident for the new groups
                var inc = new GlideRecord('incident');
                inc.initialize();
                inc.short_description = 'Groups with no members & managers';
                inc.description = 'The following groups have no members or managers:\n' + newGroups.join('\n');
                inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
                inc.insert();
            }
        }
    },

    type: 'CheckEmptyGroups'
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Viraj Hudlikar
Giga Sage

Hello @Kaustubh k 

 

Yes, you are right we are currently filtering with existing short description and if present creating incident record.

Now, thinking little more on this usecase if new group is found after next scheduled job run then ideally it should have created new incident, so modified code will be and it will ignore prevous group showcased in first inc created.
Script Include will be as below:

var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
    initialize: function() {},

checkGroups: function() {
        var gr = new GlideRecord('sys_user_group');
        gr.addActiveQuery();
        gr.query();
        
        var emptyGroups = new Set();
        
        while (gr.next()) {
            var groupId = gr.sys_id;
            var groupName = gr.name.toString();

            // Check if the group has members
            var memberCount = new GlideAggregate('sys_user_grmember');
            memberCount.addQuery('group', groupId);
            memberCount.addAggregate('COUNT');
            memberCount.query();
            memberCount.next();
            var hasMembers = memberCount.getAggregate('COUNT') > 0;

            // Check if the group has owners or managers
            var hasManager = gr.manager.toString() !== '';

            if (!hasMembers && !hasManager) {
                emptyGroups.add(groupName);
            }
        }
        
        if (emptyGroups.size > 0) {
            this.createIncidents(Array.from(emptyGroups));
        }
    },

    createIncidents: function(groupNames) {
        var existingIncident = new GlideRecord('incident');
        existingIncident.addQuery('short_description', 'Groups with no members & managers');
        existingIncident.query();
        
        var existingGroups = new Set();
        while (existingIncident.next()) {
            var existingDescription = existingIncident.description.toString();
            existingDescription.split('\n').forEach(function(group) {
                existingGroups.add(group.trim());
            });
        }

        var newGroups = groupNames.filter(function(group) {
            return !existingGroups.has(group);
        });

        newGroups.forEach(function(group) {
            var inc = new GlideRecord('incident');
            inc.initialize();
            inc.short_description = 'Groups with no members & managers';
            inc.description = 'The following group has no members or managers:\n' + group;
            inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
            inc.insert();
        });
    },

    type: 'CheckEmptyGroups'
};

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @Kaustubh k 

 

Also, if you want to avoid new incident creation you can update the existing incident by using below script include code:

var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
    initialize: function() {},
 

 checkGroups: function() {
         var gr = new GlideRecord('sys_user_group');
        gr.addActiveQuery();
         gr.query();
        
         var emptyGroups = new Set();
        
         while (gr.next()) {
             var groupId = gr.sys_id;
             var groupName = gr.name.toString();

             // Check if the group has members
             var memberCount = new GlideAggregate('sys_user_grmember');
             memberCount.addQuery('group', groupId);
             memberCount.addAggregate('COUNT');
             memberCount.query();
             memberCount.next();
             var hasMembers = memberCount.getAggregate('COUNT') > 0;

             // Check if the group has owners or managers
             var hasManager = gr.manager.toString() !== '';

             if (!hasMembers && !hasManager) {
                 emptyGroups.add(groupName);
             }
         }
        
         if (emptyGroups.size > 0) {
             this.createOrUpdateIncident(Array.from(emptyGroups));
         }
     },

     createOrUpdateIncident: function(groupNames) {
         // Check if an incident already exists for these groups
         var existingIncident = new GlideRecord('incident');
         existingIncident.addQuery('short_description', 'Groups with no members & managers');
         existingIncident.query();
         if (existingIncident.next()) {
             // Update the existing incident if it doesn't already contain the group names
             var existingDescription = existingIncident.description.toString();
             var newGroups = groupNames.filter(function(group) {
                 return !existingDescription.includes(group);
             });
             if (newGroups.length > 0) {
                 existingIncident.description += '\n' + newGroups.join('\n');
                 existingIncident.update();
             }
         } else {
             // Create a new incident
             var inc = new GlideRecord('incident');
             inc.initialize();
             inc.short_description = 'Groups with no members & managers';
             inc.description = 'The following groups have no members or managers:\n' + groupNames.join('\n');
             inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
             inc.insert();
         }
     },

    type: 'CheckEmptyGroups'
};

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hi @Viraj Hudlikar ,

 

But this is again creating multiple incidents,like 80 incidents if we have 80 groups.

@Kaustubh k 

I will suggest to use a custom field of type List referring to Group table rather than storing the group names in description

Simply update the incident and don't create daily if new group has 0 members

Did you try the script I shared above?

sharing again here

var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
    initialize: function() {},

    checkGroups: function() {
        var gr = new GlideRecord('sys_user_group');
        gr.addActiveQuery();
        gr.query();
        
         var emptyGroups = new Set();
        
        while (gr.next()) {
            var groupId = gr.sys_id;
            var groupName = gr.name.toString();

            // Check if the group has members
            var memberCount = new GlideAggregate('sys_user_grmember');
            memberCount.addQuery('group', groupId);
            memberCount.addAggregate('COUNT');
            memberCount.query();
            memberCount.next();
            var hasMembers = memberCount.getAggregate('COUNT') > 0;

            // Check if the group has owners or managers
            var hasManager = gr.manager.toString() !== '';

            if (!hasMembers && !hasManager) {
                emptyGroups.add(groupName);
            }
        }
        
        if (emptyGroups.length > 0) {
            this.createOrUpdateIncident(Array.from(emptyGroups));
        }
    },

    createOrUpdateIncident: function(groupNames) {
        // Check if an incident already exists for these groups
        var existingIncident = new GlideRecord('incident');
        existingIncident.addQuery('short_description', 'Groups with no members & managers');
        existingIncident.query();
        
        var existingGroups = new Set();
        if (existingIncident.next()) {
            // Extract existing groups from the incident description
            var existingDescription = existingIncident.description.split('\n');
            for (var i = 1; i < existingDescription.length; i++) {
                existingGroups.add(existingDescription[i]);
            }
        }

        // Find new groups that are not in the existing incident
        var newGroups = groupNames.filter(function(group) {
            return !existingGroups.has(group);
        });

        if (newGroups.length > 0) {
            if (existingIncident.isValidRecord()) {
                // Update the existing incident with new groups
                existingIncident.description += '\n' + newGroups.join('\n');
                existingIncident.update();
            } else {
                // Create a new incident for the new groups
                var inc = new GlideRecord('incident');
                inc.initialize();
                inc.short_description = 'Groups with no members & managers';
                inc.description = 'The following groups have no members or managers:\n' + newGroups.join('\n');
                inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
                inc.insert();
            }
        }
    },

    type: 'CheckEmptyGroups'
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader