Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Script to put all knowledge base categories/subcategories in an array

lmundere
Kilo Sage

I would like to write a script that pushes IDs of a specific knowledge bases into an array, taking into consideration that we can have a category, subcategory, subsubcategory etc, anyone can give me leads? The idea is to make some field mandatory on the category dependinding on what knowledge base it is inherited from.

Cheers.

1 ACCEPTED SOLUTION

Okay, try something like this, and let me know if it works for you.

 

var categoryGR = new GlideRecord('kb_category');
categoryGR.addQuery('parent_id', 'a7e8a78bff0221009b20ffffffffff17');  // sys ID of knowledge base you want to list
categoryGR.query();
var arrayCategoryValues = [];

while(categoryGR.next()){
    arrayCategoryValues.push(categoryGR.getValue('label'));
    findSubcategory(categoryGR.getUniqueValue());
}




if (arrayCategoryValues.length > 0){
    for (var i=0; i< arrayCategoryValues.length; i++){
        gs.info('Category label: ' + arrayCategoryValues[i]);
    }
}
else{
    gs.info('No data found');
}


function findSubcategory(categoryID){
    var subCategoryGR = new GlideRecord('kb_category');
    subCategoryGR.addQuery('parent_id', categoryID);
    subCategoryGR.query();
    if (subCategoryGR.hasNext()){
        while(subCategoryGR.next()){
            arrayCategoryValues.push(subCategoryGR.getValue('label'));
            findSubcategory(subCategoryGR.getUniqueValue());
        }
        
    }
}

View solution in original post

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

Hi,

Can you please elaborate on your question?

What is it you want to achieve?

Knowledge categories is stored in a separate table [kb_category]
And knowledge bases is a separate table  [kb_knowledge_base]

On the Knowledge category record, there is a reference to the parent category, which can point to another category, or (if it's a top level category) point to a knowledge base record.

Hi @OlaN ,

Let me make it simple, i would like to write a logic that shows all categories/subcategories/subsubcategories that are under a Knowledge Base. Then I will build my logic on top of that

Okay, try something like this, and let me know if it works for you.

 

var categoryGR = new GlideRecord('kb_category');
categoryGR.addQuery('parent_id', 'a7e8a78bff0221009b20ffffffffff17');  // sys ID of knowledge base you want to list
categoryGR.query();
var arrayCategoryValues = [];

while(categoryGR.next()){
    arrayCategoryValues.push(categoryGR.getValue('label'));
    findSubcategory(categoryGR.getUniqueValue());
}




if (arrayCategoryValues.length > 0){
    for (var i=0; i< arrayCategoryValues.length; i++){
        gs.info('Category label: ' + arrayCategoryValues[i]);
    }
}
else{
    gs.info('No data found');
}


function findSubcategory(categoryID){
    var subCategoryGR = new GlideRecord('kb_category');
    subCategoryGR.addQuery('parent_id', categoryID);
    subCategoryGR.query();
    if (subCategoryGR.hasNext()){
        while(subCategoryGR.next()){
            arrayCategoryValues.push(subCategoryGR.getValue('label'));
            findSubcategory(subCategoryGR.getUniqueValue());
        }
        
    }
}

Excellent @OlaN 

this is the call back function I needed, cheers.