How to dynmaically display knowledge articles in a record producer based on category/subcategory?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 08:12 AM
Hello all,
I have a business requirement to display links to knowledge articles based on the category and subcategory within a record producer. I am unsure how to accomplish this goal and would like some assistance.
I've provided a mock image of the record producer form and where I'd like to display knowledge articles within the form for users to click and read the article before submitting the incident.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 08:42 AM
@glawson_pgatss I am assuming that the knowledge article is mapped to category and sub-category in kb_knowledge table and based on the selection of these fields you would like to show the KB article link within the Knowledge article link field.
To achieve this, you can create an onChange Client script on the sub-category field. This onChange client script will make a GlideAjax call to a client callable script include which fetches the KB article link on the basis of category and sub category.
Here is the sample onChange script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
// Get the current values of Category and Subcategory
var category = g_form.getValue('category');
var subcategory = g_form.getValue('subcategory');
// Ensure both Category and Subcategory are selected
if (category && subcategory) {
// Use GlideAjax to call a Script Include to fetch the KB article link
var ga = new GlideAjax('FetchKBArticle');
ga.addParam('sysparm_name', 'getKBArticleLink');
ga.addParam('sysparm_category', category);
ga.addParam('sysparm_subcategory', subcategory);
ga.getXMLAnswer(function(response) {
var kbLink = response.responseText;
if (kbLink) {
g_form.addInfoMessage('Suggested KB Article: <a href="' + kbLink + '" target="_blank">Click here to view</a>');
} else {
g_form.clearMessages();
g_form.addInfoMessage('No KB article found for the selected category and subcategory.');
}
});
} else {
g_form.clearMessages();
}
}
Here is client callable script include code.
var FetchKBArticle = Class.create();
FetchKBArticle.prototype = {
initialize: function() {},
getKBArticleLink: function(category, subcategory) {
var kbLink = '';
var kbArticleGR = new GlideRecord('kb_knowledge');
kbArticleGR.addQuery('category', category);
kbArticleGR.addQuery('subcategory', subcategory);
kbArticleGR.addActiveQuery();
kbArticleGR.orderByDesc('sys_created_on');
kbArticleGR.setLimit(1);
kbArticleGR.query();
if (kbArticleGR.next()) {
kbLink = kbArticleGR.getValue('sys_id');
// Construct the link to the KB article
kbLink = '/kb_view.do?sys_kb_id=' + kbLink;
}
return kbLink;
},
type: 'FetchKBArticle'
};
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 10:05 AM
Hi @glawson_pgatss ,
I tried your problem in my PDI and it works for me please refer below solution
Create onChange Client script and add below code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var category = g_form.getValue('category');
var ga = new GlideAjax('sn_cwf_wrkspc.populateKBArticle');
ga.addParam('sysparm_name', 'getKBArticleLink');
ga.addParam('sysparm_category', category);
ga.getXML(getKBLinkHandler);
function getKBLinkHandler(response) {
var ans = response.responseXML.documentElement.getAttribute("answer");
if (ans) {
alert(ans);
var result = ans.split(",");
var number = result[1];
g_form.addInfoMessage('I found KB Acrticle for you please check number: ' + number + ' Suggested KB Article: <a href="' + result[0] + '" target="_blank">Click here to view</a>');
} else {
g_form.clearMessages();
g_form.addInfoMessage('There is no KB article available for the selected category.');
}
}
}
Create client callable script include and add below code
getKBArticleLink: function() {
gs.info("Here 1st line");
var cat = this.getParameter("sysparm_category");
gs.info("catcat = " + cat);
var kbLink = '';
var kbGR = new GlideRecord('kb_knowledge');
kbGR.addQuery('kb_category', cat);
kbGR.addActiveQuery();
kbGR.setLimit(1);
kbGR.query();
if (kbGR.next()) {
gs.info("Inside if = " + kbGR.number);
kbLink = kbGR.getValue('sys_id');
// Construct the link to the KB article
kbLink = '/kb_view.do?sys_kb_id=' + kbLink;
}
return [kbLink, kbGR.number]+"";
},
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak