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

Thank you for your kind words!