- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2025 11:27 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 12:42 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 12:52 AM - edited 02-10-2025 12:53 AM
Hi @Kaustubh k
To create a Flow in ServiceNow that runs weekly and handles the condition you mentioned, you can use Flow Designer to automate the process. Here’s a step-by-step guide on how to build the flow:
Step-by-Step Flow Creation in ServiceNow:
1. Create the Flow in Flow Designer
2. Add a Trigger for the Flow
- Select Scheduled Trigger as this needs to run on a weekly basis.
3. Add an Action to Lookup Records in the Group Table
- Action Type: Use Lookup Records.
- Choose the Group table (sys_user_group).
- Add the following conditions:
- Manager (field) is empty. This checks for the groups where the manager is missing.
4. Create an Incident Record for Each Group
- Add an Action: Create Record.
- Choose the Incident table (incident).
- Set the following fields based on the conditions and information you have:
- Assignment Group: Set this to the group found in the previous step (use dynamic fields to reference the group record).
- Short Description: Set this field based on your requirement, such as "Incident created due to empty group manager".
- Set other necessary fields (e.g., Description, Priority, etc.) as needed.
5. Activate the Flow
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 12:54 AM
Hello @Kaustubh k
Script Include name - CheckEmptyGroups
Script as below:
var CheckEmptyGroups = Class.create();
CheckEmptyGroups.prototype = {
initialize: function() {},
checkGroups: function() {
var gr = new GlideRecord('sys_user_group');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
var groupId = gr.sys_id;
var groupName = gr.name;
// 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 hasOwner = gr.owner != '';
var hasManager = gr.manager != '';
if (!hasMembers && !hasManager) {
this.createIncident(groupName);
}
}
},
createIncident: function(groupName) {
// Check if an incident already exists for this group
var existingIncident = new GlideRecord('incident');
existingIncident.addQuery('short_description', 'Group ' + groupName + ' has 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 = 'Group ' + groupName + ' has no members & managers';
inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
inc.insert();
},
type: 'CheckEmptyGroups'
};
A scheduled job name "Check Empty Groups" as per your requirement set Run and in script we can our script include which will create incident and if already existing incident we skip that group as per logic set in script include.
var checker = new CheckEmptyGroups();
checker.checkGroups();
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 05:26 AM
Hi @Viraj Hudlikar ,
Thanks for your response and its working.But if I have 80 groups ,it is creating 80 Incidents.
Could you help me to achieve the following thing:
It should create only 1 incident with all the 80 group details.
Thanks in Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 06:02 AM
Hello @Kaustubh k
Since your use-case has changed so the modified code 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 = [];
while (gr.next()) {
var groupId = gr.sys_id;
var groupName = gr.name;
// 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 != '';
if (!hasMembers && !hasManager) {
emptyGroups.push(groupName);
}
}
if (emptyGroups.length > 0) {
this.createIncident(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: ' + groupNames.join(', ');
inc.assignment_group = 'your_group_sys_id'; // Replace with the sys_id of the Admins group
inc.insert();
},
type: 'CheckEmptyGroups'
};
Calling of script include will be same as shared previously.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 11:24 PM
Hi @Viraj Hudlikar ,
Thanks for the response
I missed this yesterday, today when I was checking found that,it involves the same group name
Output: Groups with no members & managers: O2C_GBO_Inter-IntraCompany Billing_UK, O2C_GBO_Inter-IntraCompany Billing_UK, O2C_GBO_Inter-IntraCompany Billing_UK,
And also I need the output ,each group in separate lines,so it is easy to understand.
Thanks