How to restrict knowledge articles by location
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2015 03:59 AM
Hello all,
We got a request to create a function to show up the knowledge article search results base on the current login user's location.
For exampe, the current login user located in China, then when he searches the knowledge articles, only the articles located in China will show up.
I new created a column in kb_knowledge named u_location referenced to the Location table, used to record the articles' location. And next, I get the current login user's location by using gs.getUser().getLocation(), I think we can set the two locations the same to meet the requirement. Unfortunately looks like it still does not work.
I'm running out of ideas as this is the first time I am using servicenow. Can anyone pls help to check and let me know how to make it happen? Thanks in advance.
Here are the scripts I put in kb_knowledge ACL:
var kb = new GlideRecord('kb_knowledge');
var LoginuserLocation = gs.getUser().getLocation();
if (kb.u_location == gs.getUser().getLocation())
kb.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2015 01:15 PM
I've had mixed results using an ACL, but a Query Business Rule worked for me on all the possible interfaces for Knowledge. If you try to move your ACL code to a query BR that might work for you.
-Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2015 12:55 AM
Hi Chris,
Thanks for your reply. I new created a BR and put the script in it. But looks like does not work for me.
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
var kb = new GlideRecord('kb_knowledge');
var LoginuserLocation = gs.getUser().getLocation();
if (kb.u_location == gs.getUser().getLocation()){
kb.query();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 06:50 AM
I think your issue is actually your Query BR code. Here is the example from the "incident query" Business Rule:
if (!gs.hasRole("itil") && gs.isInteractive()) {
var u = gs.getUserID();
var qc = current.addQuery("caller_id", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u);
gs.print("query restricted to user: " + u);
}
You'll notice that it's just using the current.addQuery() method and not actually running "query()". So it should look something like:
current.addQuery('u_location', gs.getUser().getLocation());
That is it as it should be a Query Business Rule and you don't have to define the GlideRecord since it should already be defined on kb_knowledge.
-Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2015 07:19 AM
Hi Chris,
Yes, there is something wrong with my previous script. I have simply achieved my requirement via ACL with below code now:
var answer = false;
var u = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',u);
gr.query();
while (gr.next()) {
if (gr.u_region == current.u_location)
{
answer = true;
}
}
But the new problem is, currently I set both the u_region in sys_user and u_location in kb_knowledge as string type of columns, actually I think the two columns type should be set as reference, which all refered to location table. So now I am working on how to write the scripts to compare the reference type of columns in servicenow.
Cheers!