[SalesForce] Can’t access child Data Categories

I'm trying to get all Data Categories from an Article Type through the varius describe calls I've found in the documentation such as https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_DescribeDataCategoryGroupStructureResult.htm

This is the code I'm using, running it through Anonymous Apex

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

DataCategoryGroupSobjectTypePair pair1 = 
      new DataCategoryGroupSobjectTypePair();
pair1.setSobject('KnowledgeArticleVersion');
pair1.setDataCategoryGroupName('MyArticles');

pairs.add(pair1);

List<Schema.DescribeDataCategoryGroupStructureResult>results = 
      Schema.describeDataCategoryGroupStructures(pairs, true);
          for(Schema.DescribeDataCategoryGroupStructureResult r : results){
              System.debug(r.getName()); //'MyArticles'
              Schema.DataCategory[] topCategories = r.getTopCategories();
              for(Schema.DataCategory c : topCategories){
                  System.debug(c.getName()); //'All'
                  Schema.DataCategory[] subCats = c.getChildCategories();
                  System.debug(subCats); //empty, why?
                  for(Schema.DataCategory sc: subCats){
                      System.debug(sc.getName()); //Never reached
                  }
              }
          }

My Data Categories are structured with one 'All' Category at the top level, which I can find using getTopCategories(), but when I call getChildCategories()on the result, I get an empty list, despite the fact that I have 5 child categories set up under it. I made sure Data Category Visibility is on, but I still get no results. Is there any other reason I wouldn't be able to see these subcategories?

Best Answer

It's because the ",true)" in the method call for describeDataCategoryGroupStructures stands for "topCategoriesOnly"

System.Schema.describeDataCategoryGroupStructures

Turn this to false, and you should be good to go 🤙

Related Topic