Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

knowledge Base User Criteria Based on Account's Sold Products and Custom Sub-Product Field.

VigneshH
Tera Contributor

We have a requirement to control Knowledge Base visibility using User Criteria based on the logged-in user’s account and the products sold to that account.

  • Tables involved:
    • customer_contact → Stores user and their associated account.
    • sn_install_base_sold_product → Stores sold products for each account.
    • kb_knowledge_base → Knowledge Base table with a custom field u_kb_view (reference to sub-product table u_sub_product).

Requirement

When a user logs in:

  • Identify their account from customer_contact.
  • Fetch all sold products for that account from sn_install_base_sold_product.
  • Compare these sold products with the sub-product (u_kb_view) defined on the Knowledge Base. 
  • If there is a match, the user should have access to that KB.

Example:

  • Account Temple has sold products: A, B, C, D.
  • KB has u_kb_view = B.
  • Any user linked to Temple should see KB articles under that KB.

Solution Approach

(function() {
    var answer = false;

    var currentUser = user_id; // we tried using the gs.getUserid(); as wll

    // Get user's account from customer_contact
    var gr = new GlideRecord('customer_contact');
    gr.addEncodedQuery('sys_id=' + currentUser);
    gr.query();
    var account = '';
    if (gr.next()) {
        account = gr.getValue('account');
    }
    if (!account) {
        return false;
    }

    // Get KB sub-product (example: KB with title 'Generic')
    var ga = new GlideRecord('kb_knowledge_base');
    ga.addEncodedQuery('title=Generic'); // Replace with dynamic KB context if needed
    ga.query();
    var kbSubProduct = '';
    while (ga.next()) {
        kbSubProduct = ga.getDisplayValue('u_kb_view');
    }
    if (!kbSubProduct) {
        return false;
    }

    // Check sold products for this account
    var soldProductGR = new GlideRecord('sn_install_base_sold_product');
    soldProductGR.addQuery('account', account);
    soldProductGR.query();

    while (soldProductGR.next()) {
        if (soldProductGR.name.toString() == kbSubProduct.toString()) {
            answer = true;
         
        }
    }

    return answer;
})();

 

0 REPLIES 0