- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:07 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 12:13 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:18 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:25 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2023 09:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2023 12:13 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader