
sachin_namjoshi
Kilo Patron
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-13-2025 05:21 PM
Problem Statement:
I was working on a Employee Center Pro implementation project.
As part of EC pro implementation project, I wanted to ask business stakeholders to map their existing service catalog categories to taxonomies.
There is no OOB API available which returns all nested parent SC categories for existing child categories.
There is one script include available OOB for doing same for KB categories. But, it's not available for doing same for SC categories.
Solution:
I have created below script which returns arrays of nested parent categories for a given service catalog category sys_id.
function getNestedParentCategories(categoryId) {
/**
* Returns an array of nested parent categories for a given Service Catalog category sys_id.
*
* Args:
* categoryId (String): The sys_id of the Service Catalog category.
*
* Returns:
* Array: An array of category objects, starting with the immediate parent
* and going up to the top-level parent. Returns an empty array if
* the category has no parent or if the category is not found.
* Each object in the array will have 'sys_id' and 'title' properties.
*/
var parentCategories = [];
var currentCategory = new GlideRecord('sc_category');
if (currentCategory.get(categoryId)) {
var parentId = currentCategory.getValue('parent');
while (parentId) {
var parentCategory = new GlideRecord('sc_category');
if (parentCategory.get(parentId)) {
parentCategories.push({
'sys_id': parentCategory.getValue('sys_id'),
'title': parentCategory.getValue('title')
});
parentId = parentCategory.getValue('parent');
} else {
// Handle case where parent category is not found (shouldn't happen in normal scenarios)
gs.error('getNestedParentCategories: Parent category with sys_id ' + parentId + ' not found.');
break;
}
}
} else {
gs.error('getNestedParentCategories: Category with sys_id ' + categoryId + ' not found.');
}
return parentCategories;
}
var categorySysId;
// Example usage:
var cat = new GlideRecord('sc_category');
cat.addEncodedQuery('active=true^parentISNOTEMPTY');
cat.query();
while(cat.next()){
categorySysId = cat.sys_id; // Replace with the actual sys_id of your category
var parents = getNestedParentCategories(categorySysId);
if (parents.length > 0) {
gs.info('Nested Parent Categories for ' + categorySysId + ':');
for (var i = 0; i < parents.length; i++) {
gs.info(' - ' + parents[i].title + ' (' + parents[i].sys_id + ')');
}
} else {
gs.info('No parent categories found for ' + categorySysId + '.');
}
}
Labels: