- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 02:35 AM
Hi @Viraj Hudlikar ,
This is in Continuation to the Solution which we discussed in the below link:
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 06:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 02:52 AM
please share what debugging have you done so far and what's your latest script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 02:57 AM
Below is mine latest script:
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' };
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:01 AM
So how are you calling the above script and from where?
also if it's from scheduled job then it will only create 1 incident all the time, because you are checking if there is already an incident with that short description, if yes then don't create
better to add this check as well
// Check if an incident already exists for these groups
var existingIncident = new GlideRecord('incident');
existingIncident.addQuery('short_description', 'Groups with no members & managers');
existingIncident.addQuery('assignment_group', 'your_group_sys_id');
existingIncident.query();
if (existingIncident.next()) {
return; // Incident already exists
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:08 AM
Hi @Ankur Bawiskar ,
Yes it is being called from scheduled job,
1 incident is getting created,but the issue is.
If that previous incident has the following description:
Group A
Group B
Group C
But after 1 day when the job runs again, we have another Group D ,which also does not have any members or managers.So,system should create a new incident with only Group D as the description,it should not include Group A,Group B,Group C.
But as in script we are just checking the short description,so system is not creating any other incidents also