1

As per Salesforce documentation, 5 levels of categories are supported. Following code is giving information only about category with name Group_Category, but no information about child categories are returned. Is there a way to pull information about child categories using describe call.

List <DataCategoryGroupSobjectTypePair> pairs = new List<DataCategoryGroupSobjectTypePair>();
DataCategoryGroupSobjectTypePair pair = new DataCategoryGroupSobjectTypePair();
pair.setSobject('KnowledgeArticleVersion');
pair.setDataCategoryGroupName('Group_Category');
pairs.add(pair);

List<Schema.DescribeDataCategoryGroupStructureResult> groupResult = Schema.describeDataCategoryGroupStructures(pairs, true);
System.debug('groupResult:::' + groupResult);
2

Yes you can get all the child level categories. i have already done to get all the levels of category using the following code.

public List<DescribeDataCategoryGroupStructureResult> getDescribeDataCategoryGroupStructureResults(){
    List<DescribeDataCategoryGroupResult> describeCategoryResult;
    List<DescribeDataCategoryGroupStructureResult> describeCategoryStructureResult;
    try {
        //Making the call to the describeDataCategoryGroups to
        //get the list of category groups associated
        List<String> objType = new List<String>();
        objType.add('KnowledgeArticleVersion');
        describeCategoryResult = Schema.describeDataCategoryGroups(objType);

        //Creating a list of pair objects to use as a parameter
        //for the describe call
        List<DataCategoryGroupSobjectTypePair> pairs = new List<DataCategoryGroupSobjectTypePair>();

        //Looping throught the first describe result to create
        //the list of pairs for the second describe call
        for(DescribeDataCategoryGroupResult singleResult : describeCategoryResult){
           DataCategoryGroupSobjectTypePair p = new DataCategoryGroupSobjectTypePair();
           p.setSobject(singleResult.getSobject());
           p.setDataCategoryGroupName(singleResult.getName());
           pairs.add(p);
         }

        //describeDataCategoryGroupStructures()
        describeCategoryStructureResult = Schema.describeDataCategoryGroupStructures(pairs, false);

         //Getting data from the result
         for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){
            //Get name of the associated Sobject

            //Get the name of the data category group
            singleResult.getName();

            //Get the top level categories
            DataCategory [] toplevelCategories =  singleResult.getTopCategories();

            //Recursively get all the categories
            List<DataCategory> allCategories = getAllCategories(toplevelCategories);

            for(DataCategory category : allCategories) {
               //Get the name of the category
               category.getName();

            }
         }
       } 
       catch (Exception e){

        }
        System.debug(categoryToSubCategoryMap);
        return describeCategoryStructureResult;
   }

   public  DataCategory[] getAllCategories(DataCategory [] categories){
      if(categories.isEmpty()){
         return new DataCategory[]{};
      } else {
         DataCategory [] categoriesClone = categories.clone();
         DataCategory category = categoriesClone[0];
         DataCategory[] allCategories = new DataCategory[]{category};
         categoriesClone.remove(0);
         categoriesClone.addAll(category.getChildCategories());
         allCategories.addAll(getAllCategories(categoriesClone));
         return allCategories;
      }
   }

public class JSONCategory{
    public String label{set;get;}
    public String name{set;get;}
    public List<TopLevelCategory> topCategories{set;get;}
}

public class TopLevelCategory{
    public List<ChildLevelCategory> childCategories{set;get;}
}

public class ChildLevelCategory{
    public List<ChildLevelCategory> childCategories{set;get;}
    public String label{set;get;}
    public String name{set;get;}
}


List<JSONCategory> categories = (List<JSONCategory>)System.JSON.deserialize(JSON.serialize(getDescribeDataCategoryGroupStructureResults());
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.