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

Utpal Dutta
Tera Guru

Hi Sachin,

If you're working in Server Side script the GlideServer Api will help you out gs.getUser().isMemberOf('group_name').

But if you're working in Client Side script then you have to Use GlideAjax and Script Include and then check whether user is member of that group.

 

Let me know if you need more help. If you find my answer helpful then please mark it Helpful or Correct.

 

Thanks,

Utpal Dutta

Hi Utpal,

 

Thanks for sharing, this method help me to know whether is part of itil group or not. but i want to know whether is part of only itil and not any other group

 

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

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