- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 01:34 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 04:17 AM
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());
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 01:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 03:45 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 04:17 AM
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());
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 04:38 AM
Excellent @OlaN
this is the call back function I needed, cheers.