Report on groups containing all inactive members

Dolly M
Tera Guru

Need a report on groups containing all inactive members.

1 ACCEPTED SOLUTION

Thanks all -

 

Got this working using below code -

 

inactiveUserCount: function() {
var arr = [];
var group = new GlideRecord('sys_user_group');
group.addEncodedQuery('active=true^typeNOT LIKEd317d8111b537cd096c5a970f54bcbaf');
group.query();
while (group.next()) {
var group_member = new GlideAggregate('sys_user_grmember');
group_member.addEncodedQuery('group.sys_id=' + group.sys_id);
group_member.query();
var member_count = group_member.getRowCount();

if (member_count > 0) {
var active_member = new GlideAggregate('sys_user_grmember');
active_member.addEncodedQuery('group.sys_id=' + group.sys_id + '^user.active=false');
active_member.query();
var active_member_count = active_member.getRowCount();
if (active_member_count == member_count) {
arr += (group.sys_id + ',');
}
}
}
return arr;
}

View solution in original post

16 REPLIES 16

@Dolly M it is not iterating through all group members records btw.

 

But let me optimize the code and come back here 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Ankur Bawiskar
Tera Patron
Tera Patron

@Dolly M 

create a client callable script include which is classless and check this

function getGroups(){
	var arr = [];
	var groupRec = new GlideRecord("sys_user_grmember");
	groupRec.query();
	while(groupRec.next()){
		var group = groupRec.getValue('group');
		var grpMemberGr = new GlideRecord("sys_user_grmember");
		grpMemberGr.addQuery("group", group);
		grpMemberGr.addQuery('user.active', true);
		grpMemberGr.query();
		if(!grpMemberGr.hasNext()){
			arr.push(groupRec.group.toString());
		}
	}
	return arr.toString();
}

AnkurBawiskar_3-1685524775329.png

 

This is how you will call the script include

Report will be on Group table and give this filter condition

AnkurBawiskar_1-1685524645162.png

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

 

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 

I ran the report but the server became unresponsive and the page got hanged as we have high no. of data in group member table. Also I don't think there is any convenient way to solve this issue. 

 

Basically, this was a requirement from higher management to see groups which have all members as inactive. But there's a lot that needs to be done at the backend because the query/logic has to be ran against each record in sys_user_grmember.

 

@Dolly M 

possibly the query is taking time.

So you better do it for few records first and then use the complete script.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, 

For few records it is working fine. But not working for all records.

By any chance can it be done via Database view ?