- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 12:58 PM - edited 01-30-2024 01:04 PM
Here is the code that I am using but it is not working. What is wrong with it?
// Check if the current user belongs to a group starting with "CIS"
(function() {
// Get the current user's groups
var userGroups = gs.getUser().getMyGroups();
// Loop through the groups to check if any starts with "CIS"
for (var i = 0; i < userGroups.size(); i++) {
var groupName = userGroups.get(i).getName();
if (groupName.startsWith("CIS")) {
// User belongs to a group starting with "CIS", allow access
return true;
}
}
// User doesn't belong to any group starting with "CIS", deny access
return false;
})();
The faulty result for this script is that both users whom have the group and whom not have the group, have no access to the catalog item.
Please help!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 07:44 PM
Hi @gracjan
Let's give my adjustment a try.
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user_id);
grMember.addQuery('group.name', 'STARTSWITH', 'CIS');
grMember.setLimit(1);
grMember.query();
answer = grMember.hasNext();
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 06:46 PM
Good day !!
If you are writting the script in user criteria then gs.getUser() not recommended.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 07:44 PM
Hi @gracjan
Let's give my adjustment a try.
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user_id);
grMember.addQuery('group.name', 'STARTSWITH', 'CIS');
grMember.setLimit(1);
grMember.query();
answer = grMember.hasNext();
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2024 05:24 AM
Brilliant. Thank you!