Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Kaustubh k
Tera Expert

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

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.