How to write a script section in USER CRITERIA for access to knowledge base;

ganesh68
Kilo Contributor

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.

10 REPLIES 10

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

LinkedIn

asifnoor
Kilo Patron

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.

Hello Ganesh

Let me know if this has answered the question or if you are still facing any issue.

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

LinkedIn

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.