Getting multiple findings for a script-only check on Instance Scan

Community Alums
Not applicable

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);

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I just created quickly a Table Check, works fine. It's a combination of a Table Check with a condition + script.

find_real_file.png

(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

LinkedIn

View solution in original post

6 REPLIES 6

RAHUL YADAV9
Mega Guru

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.

Community Alums
Not applicable

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.

suvro
Mega Sage
Mega Sage

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 Roethof
Tera Patron
Tera Patron

Hi there,

I just created quickly a Table Check, works fine. It's a combination of a Table Check with a condition + script.

find_real_file.png

(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

LinkedIn