Fetching Only latest version of Knowledge article in Catalog item variable

Rakesh40
Tera Contributor

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

6 REPLIES 6

@Rakesh40 

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.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Rakesh40 

try with what @J Siva has shared and see if that helps.

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