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

Ankur Bawiskar
Tera Patron
Tera Patron

@Kaustubh k 

you can have a scheduled flow and use Lookup Records on sys_user_group

1) check in sys_user_grmember if no member using Lookup Record

2) check in group has no owner or no manager

3) if all 3 checks are true then create an incident for that group using Create Record

What did you start with and where are you stuck?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Kaustubh k 

you can also have a scheduled job which runs daily with this script, please enhance as per your requirement

// Scheduled Script to check groups and create incidents if necessary
var grGroup = new GlideRecord('sys_user_group');
grGroup.query();

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

    // Check for members
    var grMember = new GlideRecord('sys_user_grmember');
    grMember.addQuery('group', groupId);
    grMember.query();
    var hasMembers = grMember.hasNext();

    // Check for owners
    var hasOwners = grGroup.u_owner != ''; // Assuming 'u_owner' is the custom field for owner


    // Check for managers
    var hasManagers = grGroup.manager != '';
    // If no members, owners, or managers, create an incident
    if (!hasMembers && !hasOwners && !hasManagers) {
        // Check if an incident already exists for this group
        var grIncident = new GlideRecord('incident');
        grIncident.addQuery('u_group', groupId); // Assuming 'u_group' is a custom field to track the group
        grIncident.addQuery('state', '!=', '7'); // Exclude resolved incidents
        grIncident.query();
        if (!grIncident.hasNext()) {
            // Create a new incident
            var newIncident = new GlideRecord('incident');
            newIncident.initialize();
            newIncident.short_description = 'Group ' + groupName + ' has no members, owners, or managers';
            newIncident.assignment_group.setDisplayValue('Admins'); // Set the Admins group
            newIncident.u_group = groupId; // Set the custom field to track the group
            newIncident.insert();
        }
    }
}

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

Hi @Ankur Bawiskar ,

 

If I have 80 groups system is creating 80 incidents,I want only one incident to be created with all the group details.

@Kaustubh k 

then check this link where I shared solution on how to send 1 email per user, enhance it further for your requirement

Email script 

Email Notification to all users of assets assigned specifically to them 

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