- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 12:34 AM
I want to define a Script Only check in Instance Scan, where it should point out all those groups which have no users.
I am getting the name of the group which doesn't have any users, but getting multiple findings for the same. Multiple findings, that too for the same record.
Please look at my script, and see if I can change anything in the script. Because according to me, there is some issue with the script.
Here's the script below:
(function(finding) {
// Add your code here
var group = new GlideAggregate('sys_user_group');
var group_members = new GlideRecord('sys_user_grmember');
group.addQuery('active', 1);
group.addAggregate('COUNT');
group.query();
while (group.next()) {
var no_of_groups = group.getAggregate('COUNT');
var group_names = [];
group_names.push(group.name.toString());
}
for (var i = 0; i < no_of_groups - 1; i++) {
group_members.addQuery('group', group_names[i]);
group_members.query();
if (!group_members.next()) {
finding.setCurrentSource(group);
finding.increment();
}
}
})(finding);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 04:01 AM
Hi there,
I just created quickly a Table Check, works fine. It's a combination of a Table Check with a condition + script.
(function (engine) {
var group_members = new GlideRecord('sys_user_grmember');
group_members.addQuery('group', engine.current.getUniqueValue());
group_members.setLimit(1);
group_members._query();
if(!group_members.hasNext()) {
engine.finding.increment();
}
})(engine);
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 12:48 AM
Here is the script to get all the active groups where there are no members:
var queryString = "active=true";
var now_GR = new GlideRecord('sys_user_group');
now_GR.addEncodedQuery(queryString);
now_GR.query();
while (now_GR.next()) {
var now_GR1 = new GlideRecord('sys_user_grmember');
now_GR1.addQuery('group',now_GR.getUniqueValue());
now_GR1.query();
if(!now_GR1.hasNext())
{
gs.print('Group has no members : ' + now_GR.name);
}
}
Feel free to mark correct and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 03:50 AM
The condition is correct, if we want to determine all the groups with no users. But, I have a different requirement. I have to create a check in the instance scan. And in that, the findings are not accurate. Please let me know if you can help with that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 03:59 AM
Try below code
var count = 0;
var group_names = [];
var group = new GlideRecord('sys_user_group');
group.addQuery('active', true);
group.query();
while (group.next()) {
count = count + 1;
group_names.push(group.sys_id.toString());
}
for (var i = 0; i < count; i++) {
var group_members = new GlideRecord('sys_user_grmember');
group_members.addQuery('group', group_names[i]);
group_members.query();
if (!group_members.next()) {
finding.setCurrentSource(group_names[i]);
finding.increment();
}
}
})(finding);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 04:01 AM
Hi there,
I just created quickly a Table Check, works fine. It's a combination of a Table Check with a condition + script.
(function (engine) {
var group_members = new GlideRecord('sys_user_grmember');
group_members.addQuery('group', engine.current.getUniqueValue());
group_members.setLimit(1);
group_members._query();
if(!group_members.hasNext()) {
engine.finding.increment();
}
})(engine);
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field