Script to check if a group has only one user and the user is a given user

Karthick Nagara
Tera Expert

Hi ,

Looking for suggestions to improve this script.

I have a requirement to know if a group has only one member in it and that member is a Requested by user.

For this, I created the below script include which is taking userID & GroupID as inputs and checks if the given user is the only available member of the group.

Any suggestions to achieve this without using a GlideRecord inside GlideAggregate...

	isUser_the_OnlyMember: function(userID,groupID)
	{

	var grCheckMemCount = new GlideAggregate('sys_user_grmember');
	grCheckMemCount.addQuery('group', groupID);
    grCheckMemCount.addQuery('user.active',true);
	grCheckMemCount.addAggregate('COUNT');
    grCheckMemCount.query();

	if(grCheckMemCount.next()) 
    {
		if(grCheckMemCount.getAggregate('COUNT') == 1)
		{

		 gs.print("Group has only one member");
//Creating a GlideRecord since Im not able access any value of grCheckMemCount after the getAggregate.
			var grUserIsOnlyMem = new GlideRecord('sys_user_grmember');
			grUserIsOnlyMem.addQuery('group',groupID);
			
			grUserIsOnlyMem.query();

			if(grUserIsOnlyMem.next())	
			{
				if(grUserIsOnlyMem.user == userID)
				return true;
				else
				{return false;}		
			}	
		}
		else{
			return false;
		}
	}
1 ACCEPTED SOLUTION

Hi,

got it

then the only way is what you mentioned OR the other approach I mentioned

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi,

got it

then the only way is what you mentioned OR the other approach I mentioned

Regards
Ankur

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

@Karthick Nagarajan 

Thank you for marking my response as helpful.

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

Regards
Ankur

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