How to write a script section in USER CRITERIA for access to knowledge base;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 02:14 AM
I have a article A in my knowledge base. User with a group X. shouldnt have access to the KB article A.
I have written script for the same. Can some one help me whats the issue ?
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', 'user_id');
gr.addQuery('group', '<Sys id of group name>');
gr.query();
if (gr.next()) {
gs.info("User is Member of Group! requester");
answer= true;
} else {
gs.info("User is Not a Member of Group!");
answer= false;
}
this code is not working it seems.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 03:12 AM
Good one, nice that you shared. Didn't notice before.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 02:40 AM
Hi,
if user_id is a variable, then do not use single quotes.
var user_id=gs.getUserID();
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', user_id);
gr.addQuery('group', '<Sys id of group name>');
gr.query();
if (gr.next()) {
gs.info("User is Member of Group! requester");
answer= true;
} else {
gs.info("User is Not a Member of Group!");
answer= false;
}
Alternatively you can try like this
if(gs.getUser().isMemberOf("group name")) {
answer= true;
} else {
gs.info("User is Not a Member of Group!");
answer= false;
}
Mark the comment as correct/helpful if it helps to solve the problem.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 04:29 AM
Hello Ganesh
Let me know if this has answered the question or if you are still facing any issue.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 04:37 AM
Ganesh mentioned not wanting to use gs.getUserID();
Also this should not be necessary, as the User Criteria already contains user_id by default as variable.
The Docs page also mentions this. I actually didn't knew, though good point from Ganesh.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2020 04:42 AM
Aha. Just went through the thread and the link. Got it.
so just removing the quotes (as I have mentioned in my script) from the user_id variable in the query should be enough.