Advance view rules script for knowledge

Apaul
Tera Contributor

I'm working on a View Rule in ServiceNow where I need to display the "People_Zone_View" specifically when the knowledge base is "IT", and fall back to the default view for all other cases. I tried using dot-walking with current.kb_knowledge_base.name but it doesn't seem to be working as expected. Could you help me understand the correct approach for referencing the knowledge base name in a View Rule, or suggest a better way to implement this logic?

TribalChief_0-1748533682195.png


Here is the code:

(function overrideView(view, is_list) {
    var answer = view; // Default to the current view

    // Access the knowledge base record
    var kb = new GlideRecord('kb_knowledge_base');
    kb.addQuery('sys_id', current.kb_knowledge_base);
    kb.query();

    if (kb.next()) {
        // Check if the knowledge base name is "IT"
        if (kb.name == "IT") {
            answer = "People_zone_view";
        }
    }

    return answer;

})(view, is_list);

Can anyone help me to solve?
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Apaul 

view rule runs before the list/form is loaded and hence it doesn't have access to current object

For your requirement you will have to create multiple view rules based on unique conditions.

Also check this link which talks about the workaround

The "current" object is not available in Advanced View Rule scripts although selectable in the scrip... 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I tried with this one now and still not working

(function overrideView(view, is_list) {
    var answer = view;

    // Use GlideRecord to get the knowledge base sys_id
    var kbGR = new GlideRecord('kb_knowledge_base');
    if (kbGR.get(g_request.getParameter('sys_id'))) {
        if (kbGR.kb_knowledge_base == "a7e8a78bff0221009b20ffffffffff17") {
            answer = "People_Zone_View";
        }
    }

    return answer;

})(view, is_list);

@Apaul 

you didn't get the correct script from that link

Try this and enhance and debug

(function overrideView(view, is_list) {

    // Use GlideRecord to get the knowledge base sys_id
    var url = gs.action.getGlideURI().getMap();
    var sysId = url.get('sys_id');
    var kbGR = new GlideRecord('kb_knowledge_base');
    if (kbGR.kb_knowledge_base == "a7e8a78bff0221009b20ffffffffff17") {
        answer = "People_Zone_View";
    }

})(view, is_list);

If my response helped please mark it correct and close the thread so that it benefits future readers. 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Apaul 

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader