Before Business Rule for Knowledge Articles

cmills_1128
Kilo Contributor

I'm attempting to edit the script from this article but it's not working.: https://community.servicenow.com/community?id=community_blog&sys_id=264e66addbd0dbc01dcaf3231f96196c

I'd like to limit access based on a different field on our user records.

Field on my Knowledge Articles:

find_real_file.png

Field on the User Records:

find_real_file.png

The Business Rule is a before query business rule.  The script is below.

// customer would have added a field called 'u_role'
// (instead of checking an article list)
// and each article might have this field that can be checked
// against the user's role
//
// Exceptions: users with role 'admin' or 'knowledge_admin' or 'knowledge_manager'
//     will be able to see all article (not restricted by role)
function onBefore(current, previous) {
    //This function will be automatically called when this rule is processed
      var userObj = gs.getUser();
if(!userObj.hasRole("admin") && !userObj.hasRole("knowledge_admin") && !userObj.hasRole("knowledge_manager")) {
var gr = new GlideRecord("sys_user");
gr.get(gs.getUserID());
var currentUserRole = gr.u_vsc_principal;
var q=current.addNullQuery('u_role');
q.addOrCondition('u_role', currentUserRole);
gs.print("Query restricted to role: " + currentUserRole);
}
}
4 REPLIES 4

Shane J
Tera Guru

Why aren't you using an ACL instead?

From what I've read that can cause issues with search for version 3 KBs.  So a user would see the result if they searched the KB but wouldn't be able to read the article.

https://docs.servicenow.com/bundle/kingston-servicenow-platform/page/product/knowledge-management/reference/r_MigratingKnowledgeAccess.html

Ok.  Is your gs.print working or is it failing before that?  If it's failing earlier, add some more of those earlier to try to determine where it's failing.

 

I'm not sure why you're not just using gs.hasRole() for these?

 

      var userObj = gs.getUser();
if(!userObj.hasRole("admin") && !userObj.hasRole("knowledge_admin") && !userObj.hasRole("knowledge_manager")) {