Same category widget for knowledge base

Kvist
Tera Contributor

Hi!

 

I was trying, through the aid of AI, to create a widget for knowledge base that lists other articles with the same category as the article currently viewed (the widget is meant for the knowledge article view page). However, it doesn't work. Can anyone see what is wrong here?

HTML:

<div class="panel panel-default b panel-wrapper">
<div class="panel-heading b-b widget-header">
<h3>Related articles</h3>
<ul>
<li ng-repeat="article in data.related_articles">
<a ng-href="/kb_knowledge.do?sys_id={{article.sys_id}}">{{article.title}}</a>
</li>
</ul>
</div>
</div>

 

Server script:

(function() {
var currentArticle = $sp.getParameter('sys_id');
var currentCategory = '';

// Get the current article record
var articleGR = new GlideRecord('kb_knowledge');
if (articleGR.get(currentArticle)) {
currentCategory = articleGR.category;
}

// Only proceed if a category was found
if (currentCategory) {
// Query for articles in the same category
var relatedArticles = [];
var relatedGR = new GlideRecord('kb_knowledge');
relatedGR.addQuery('category', currentCategory); // Ensure this matches the category type
relatedGR.addQuery('sys_id', '!=', currentArticle); // Exclude the current article
relatedGR.query();

while (relatedGR.next()) {
relatedArticles.push({
title: relatedGR.short_description.toString(),
sys_id: relatedGR.sys_id.toString(),
});
}

data.related_articles = relatedArticles;
} else {
// If no category, set an empty array
data.related_articles = [];
}
})();

7 REPLIES 7

Hi @Kvist ,

 

where you added this widget ?.because widget takes the sys_id value from url if you don't have that then it wont return anything 

 

thats why i mentioned "after that you need to place this is in knowledge article view page like from OOTB we have "kb_article_view"" - in this page it has url contians sys_id so that it will display.

 

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

BK

Kvist
Tera Contributor

I have placed it in a clone of the kb_article_view page.

hi @Kvist ,

 

can you share the screenshot of page 

 

Thanks,

BK