Having issue with Query business rule on Knowledge Article table

Musab Rasheed
Tera Sage
Tera Sage

Hello Experts,

I have a requirement to show knowledge articles to users who are satisfying can read and cannot read criteria , I'm doing this to get rid of 'Security constraints' message which is annoying to customers, I have written query business rule to query can read and cannot read fields but it is not working 100%. kindly help to identify issue here. Below is the code.

I have considered below scenarios to write this BR.

1) Can Read , Cannot read - Empty.

2) Can Read - Empty , Cannot read - Not empty.

3) Can read - Not empty , Cannot read - Empty.

4) Can Read - Not empty , Cannot read - Not empty.

 

(function executeRule(current, previous /*null when async*/ ) {

var allUserCriteria = SNC.UserCriteriaLoader.getAllUserCriteria();

//Can read not empty
var canReadQuery = allUserCriteria.join('^ORcan_read_user_criteriaLIKE');
canReadQuery = 'can_read_user_criteriaLIKE' + canReadQuery;

//Cannot read not empty
var cann = allUserCriteria.join('^cannot_read_user_criteriaNOT LIKE');
cann = 'cannot_read_user_criteriaNOT LIKE' + cann;

var canempty = 'can_read_user_criteriaISEMPTY';
var cannottempty = 'can_read_user_criteriaISNOTEMPTY';
var cannotempty = 'cannot_read_user_criteriaISEMPTY';
var cannotnotempty = 'cannot_read_user_criteriaISNOTEMPTY';

var gr1 = canempty + '^' + cannotempty;
var gr2 = canempty + '^' + cannotnotempty + '^' + cann;
var gr3 = cannotempty + '^' + cannottempty + '^' + canReadQuery;
var gr4 = cannottempty + '^' + cannotnotempty + '^' + canReadQuery + '^' + cann;

var err = gr1 + '^NQ' + gr2 + '^NQ' + gr3 + '^NQ' + gr4;

current.addEncodedQuery(err);

})(current, previous);

Please hit like and mark my response as correct if that helps
Regards,
Musab
1 ACCEPTED SOLUTION

Disclaimer: An ACL is definitely the recommended way to do this! The method below could be very inefficient depending on the number of records in your kb_knowledge table.

If I'm understanding correctly, you just need to query based on the sys_ids in the kb_knowledge table that the current user can read, right? You should be able to leverage the GlideRecord.canRead() function here. Get all articles, then iterate them and return the sys_ids of the ones where kb.canRead() is true.

(function executeRule(current, previous) {
  var allowedIds = [];

  var kb = new GlideRecord('kb_knowledge');

  kb.query();

  while (kb.next()) {
    if (kb.canRead()) {
      allowedIds.push(kb.getUniqueValue());
    }
  }

  current.addEncodedQuery('sys_idIN' + allowedIds);
})(current, previous);

View solution in original post

13 REPLIES 13

@David Arbour : Printing something like this

 

find_real_file.png

Please hit like and mark my response as correct if that helps
Regards,
Musab

Based on what you've told me, this looks correct. This will return all articles that do not have a class of kb_knowledge_block and are in the list of sys_ids in your logging statement.

Since this is a query Business Rule, this is what will always return for the table, at a minimum. If you want to change the records returned based on role, you'll need to add some conditions in your code to account for that. In the "while" statement in the code I provided, if you want to add additional conditions there, you can. Ultimately you only want to push the sys_ids of the articles you want to display when the user views the table.

Very simply put, whatever sys_ids you push into the allowedIds array will display in the list. So if you want to conditionally exclude some sys_ids, add logic to not allow those to be pushed to the array.

@David Arbour : Thanks for the explanation, as of now whatever code you have given seems to be working fine, Will tag again in case I have any query, Many thanks for your help.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hello!

I tried the above code and it totally worked. The only thing is querying large data is making our instance slow. Do you have any suggestions?

Thankyou!