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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could use the isMemberOf method for this.

gs.getUser().isMemberOf('Group Name') or gs.getUser().isMemberOf('group_sys_id')

If my answer helped you in any way, please then mark it as helpful.

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

As per service now docs , 

  • Do not use gs.getUser() or other session APIs because they cause conflicts when used in diagnostic tools. Use the predefined user_id variable available in the script to get the user ID of the user being used to evaluate the script.

Can you share where on the docs that is? Havent seen that, could be interesting.

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

https://docs.servicenow.com/bundle/paris-servicenow-platform/page/product/knowledge-management/task/create-user-criteria-record-in-knowledge-management.html