Incident creation ig group does not have members or owners

Kaustubh k
Tera Contributor

Hi Team,

 

We have below requirement,

If any of the group does not have any members or owners or managers then an incident should be created and assigned to a Admins group.

 

multiple incidents should not be created for same set of groups.

 

How can we achieve this.

 

thanks

1 ACCEPTED SOLUTION

Hello @Kaustubh k 

Sorry for inconvenience caused to you. I have modified script include code and tested this time in my PDI 😉

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) {
        // 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()) {
            return; // Incident already exists
        }

        // 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.

View solution in original post

11 REPLIES 11

Hello @Kaustubh k 

Sorry for inconvenience caused to you. I have modified script include code and tested this time in my PDI 😉

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) {
        // 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()) {
            return; // Incident already exists
        }

        // 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.

Thank you so much @Viraj Hudlikar .Its working