How to check the logged in user is only part of some particular group

Sachin G K1
Kilo Sage

Hi All,

 

I want to check if the logged in user is only part of some particular group. for example, i want to check the logged in user is part of only 'itil' group.

 

 

 

Thanks in Advance,

Sachin

2 ACCEPTED SOLUTIONS

Okay so you have to do a GlideRecord query of (sys_user_grmember) table and check if the logged in user has only 1 group assigned to them. If count is more than 1 then check if the group is (itil). If the group is (itil) then return true.

 

Below is the code:

var grGroup = new GlideRecord('sys_user_grmember')
grGroup.addQuery('user',gs.getUserID());
grGroup.query();
var count = grGroup.getCount();

if(count>1)
{
return false; //or whatver you want to do
}

else
{
if(grGroup.getDisplayValue('group') == 'itil')
{
return true; //or whatever you want to do here.
}
}

 

I have written this code here so some syntax might be incorrect but logic is right. Please let me know if you need more help.

 

If my answer was helpful please mark it Helpful or Correct.

 

Thanks,

Utpal

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Sachin G K1 

where you wish to check this? on which form?

use this script and it will tell if logged in user is member of only itil group and no other group

var grGroup = new GlideRecord('sys_user_grmember')
grGroup.addQuery('user',gs.getUserID());
grGroup.addQuery('group.name', 'itilGroup');
grGroup.query();
if(grGroup.getRowCount() == 1){
	return 'true'; // it means only 1 group
}
else{
	return 'false'; // it means user belongs to some other group or user belong to multiple groups or user is not member of any group
}

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

View solution in original post

5 REPLIES 5

@Sachin G K1 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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