Search In This Blog

2025-01-07

Salesforce: Get all data categories apiName & Label

 Here is the sample code:


List<DescribeDataCategoryGroupResult> describeCategoryResult;

List<DescribeDataCategoryGroupStructureResult> describeCategoryStructureResult;

 

List<String> objType = new List<String>();

objType.add('KnowledgeArticleVersion');

describeCategoryResult = Schema.describeDataCategoryGroups(objType);

 

List<DataCategoryGroupSobjectTypePair> pairs = new List<DataCategoryGroupSobjectTypePair>();

 

for(DescribeDataCategoryGroupResult singleResult : describeCategoryResult){

    DataCategoryGroupSobjectTypePair p = new DataCategoryGroupSobjectTypePair();

    p.setSobject(singleResult.getSobject());

    p.setDataCategoryGroupName(singleResult.getName());

    pairs.add(p);

}

describeCategoryStructureResult = Schema.describeDataCategoryGroupStructures(pairs, false);

 

for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){

    System.debug(singleResult.getLabel());

    DataCategory [] toplevelCategories = singleResult.getTopCategories();

 

    DataCategory [] categoriesClone = toplevelCategories.clone();

    for(DataCategory category : categoriesClone){

        System.debug('top:' + category.getName());

        System.debug(category.getLabel());

 

        DataCategory[] childCategories = category.getChildCategories();

        for(DataCategory childCategory : childCategories){

            System.debug('childCategory:' + childCategory.getName());

            System.debug(childCategory.getLabel());

        }

    }

}