Fetching Only latest version of Knowledge article in Catalog item variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 11:53 PM
Hi Everyone,
On the Catalog item I have a variable referencing the Knowledge article (kb_knowledge) table.
At the moment, the variable retrieves all versions of KB articles where the conditions are: Active = true and Workflow = published.
For example, if there’s a KB article like KB0001234, with versions 1.0, 2.0, etc., I want to display only the latest version (2.0) in the variable dropdown.
Currently, all active and published versions are being shown, which is not the desired outcome.
Could someone help with a solution for this?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 02:10 AM
try this script include and use that in advanced ref qualifier
javascript: new KBArticleUtils().getLatestPublishedArticles();
Script Include:
var KBArticleUtils = Class.create();
KBArticleUtils.prototype = {
initialize: function() {},
getLatestPublishedArticles: function() {
var latestArticles = {};
var result = 'sys_idIN';
// Query all active, published KB articles
var gr = new GlideRecord('kb_knowledge');
// gr.addQuery('active', true);
// gr.addQuery('workflow_state', 'published');
gr.query();
// For each article number, keep only the highest version
while (gr.next()) {
var articleNumber = gr.number.toString();
var version = parseFloat(gr.version.toString());
var sysId = gr.sys_id.toString();
if (!latestArticles[articleNumber] || version > latestArticles[articleNumber].version) {
latestArticles[articleNumber] = {
sys_id: sysId,
version: version
};
}
}
// Build sys_idIN qualifier string
var sysIds = [];
for (var key in latestArticles) {
sysIds.push(latestArticles[key].sys_id);
}
if (sysIds.length > 0) {
result += sysIds.join(',');
} else {
result = ''; // No results
}
return result;
},
type: 'KBArticleUtils'
};
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 12:10 AM