Help with a scripted user critera for ESC Protal (Knowledgebase is XXX)

Community Alums
Not applicable

Hello, 

 

We are in the process of integrating IT into ESC (which already has HR) and we will be using the same portal page. On the page I have two widgets (buttons) for incident/case deflection. Log an Incident and Open a Case. 

 

I created a scripted user criteria that retrieves the sys_id from the URL, queries the knowledge table, and returns true if the u_function is 'IT'.

 

The main issue is my User criteria isn't working and both widget always show whether the knowledgebase is a IT or HR article. I'm not sure if this is the best or logical way to show/hide these widgets. But could do with some pointers if possible!

 

 

(function() {
    // Function to get URL parameter by name
    function getURLParameter(name) {
        var regex = new RegExp('[?&]' + name + '=([^&#]*)');
        var results = regex.exec(window.location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    }

    // Get the sys_id from the URL parameter
    var sys_id = getURLParameter('sys_id');

    // If sys_id is not present in the URL, deny access
    if (!sys_id) {
        return false;
    }

    // Query the kb_knowledge table with the sys_id
    var kbKnowledgeGr = new GlideRecord('kb_knowledge');
    kbKnowledgeGr.addQuery('sys_id', sys_id);
    kbKnowledgeGr.query();

    // If no record is found, deny access
    if (!kbKnowledgeGr.next()) {
        return false;
    }

    // Get the kb_knowledge_base sys_id from the kb_knowledge record
    var kbBaseSysId = kbKnowledgeGr.kb_knowledge_base;

    // Query the kb_knowledge_base table to check u_function
    var kbBaseGr = new GlideRecord('kb_knowledge_base');
    kbBaseGr.get(kbBaseSysId);

    // Check if u_function is equal to 'IT'
    if (kbBaseGr.u_function == 'IT') {
        return true; // Access granted if u_function is 'IT'
    } else {
        return false; // Access denied if u_function is not 'IT'
    }
})();

 

 

The second issue is my instance options are greyed out, although the sp_instance records exist and have the user criteria applied. Not sure if this has any impact on the main issue.

 

richardsaun_0-1722327245748.png

3 REPLIES 3

AnirudhKumar
Mega Sage
Mega Sage

I suspect your regex and getURLParameter may not be working... Have you logged it out?

Anyway, to get the sysid use this instead:

 var sys_id = $sp.getParameter("sys_id");

Community Alums
Not applicable

Thank you i updated the script.

 

I don't appear to be getting any logs whatsoever, do you know what that might be?

 

(function() {
    try {
        // Get the sys_id from the URL parameter
        var sys_id = $sp.getParameter("sys_id");

        // Log the extracted sys_id for debugging
        gs.info('RS: Extracted sys_id from URL: ' + sys_id);

        // If sys_id is not present in the URL, deny access
        if (!sys_id) {
            gs.info('RS: No sys_id found in the URL');
            return false;
        }

        // Query the kb_knowledge table with the sys_id
        var kbKnowledgeGr = new GlideRecord('kb_knowledge');
        kbKnowledgeGr.addQuery('sys_id', sys_id);
        kbKnowledgeGr.query();

        // If no record is found, deny access
        if (!kbKnowledgeGr.next()) {
            gs.info('RS: No kb_knowledge record found with sys_id: ' + sys_id);
            return false;
        }

        // Get the kb_knowledge_base sys_id from the kb_knowledge record
        var kbBaseSysId = kbKnowledgeGr.getValue('kb_knowledge_base');

        // Query the kb_knowledge_base table to check u_function
        var kbBaseGr = new GlideRecord('kb_knowledge_base');
        if (kbBaseGr.get(kbBaseSysId)) {
            // Log the u_function value for debugging
            gs.info('RS: kb_knowledge_base.u_function value: ' + kbBaseGr.getValue('u_function'));

            // Check if u_function is equal to 'IT'
            if (kbBaseGr.getValue('u_function') === 'IT') {
                gs.info('RS: User criteria satisfied');
                return true; // Access granted if u_function is 'IT'
            }
        } else {
            gs.info('RS: No kb_knowledge_base record found with sys_id: ' + kbBaseSysId);
        }

        gs.info('RS: User criteria not satisfied');
        return false; // Access denied if u_function is not 'IT'
    } catch (e) {
        gs.error('RS: Error in user criteria script: ' + e.message);
        return false;
    }
})();

 

Hmm... I think we need to break this into blocks to debug better...

But first, replace:

gs.error('RS: Error in user criteria script: ' + e.message);

 with:

gs.info('RS: Error in user criteria script: ' + e.message);

 

Give it a test and see if the catch block executes.

 

If the log appears, and the error still doesn't make sense, simplify the code.

Just execute the below:

(function() {
        // Get the sys_id from the URL parameter
        var sys_id = $sp.getParameter("sys_id");

        // Log the extracted sys_id for debugging
        gs.addInfoMessage('RS: Extracted sys_id from URL: ' + sys_id);
})();

 

Verify if the sysid appears in the infomessage on the top of the page (Almost sure it will)

Next add your code block by block... And use addInfoMessage() much better debugging tool on SP.