logo

NJP

Service Portal - Custom Widget to display KB Article in User's Language

Import · Jul 12, 2018 · article

I'm using the KB Article Page widget to display a specific KB article on my portal. That works really well until I need to display the French version of the article for users who have their language preference set to the French.

I found a solution by cloning the existing widget and adding some custom code. The code takes the sys_id passed in by the HTML page and the user's language setting.

It first looks for a published, translated version of the specified article with the matching language. If it finds that - it uses it. If there is no translated, published version, it uses the selected English article. If the user language is English, then the selected article is used. I believe this covers all possible scenarios.

var t = data;
data.kb_knowledge_page = $sp.getDisplayValue("kb_knowledge_page") || "kb_view";

//CUSTOM CODE
data.language = gs.getUser().getLanguage();  //get user language

var articleGR = GlideRecord("kb_knowledge");

if(data.language == 'fq'){

    //look for French version - must be published & lang = French
    articleGR.addQuery('parent', $sp.getParameter('sys_id'));
    articleGR.addQuery('workflow_state', 'published');
    articleGR.addQuery('language', 'fq');
    articleGR.query();

    if(articleGR.next()){   
        //got a French version - use that
        articleGR.get(articleGR.sys_id);        

    } else {
            //in case there's no French, use the English article
            articleGR.get($sp.getParameter('sys_id'));      
    }
} else {
        //get English article
        articleGR.get($sp.getParameter('sys_id'));
    }
//END CUSTOM CODE


var recordIsValid = articleGR.isValidRecord();
var canReadArticle = articleGR.canRead();
t.isvalid = recordIsValid && canReadArticle;

Hopefully someone will find this useful.

View original source

https://www.servicenow.com/community/developer-articles/service-portal-custom-widget-to-display-kb-article-in-user-s/ta-p/2329913