Specify the category group and associated object, then retrieve the categories available to this object (see describeDataCategoryGroupStructures).
The describeDataCategoryGroupStructures method returns the categories available for the object in the category group you specified. For additional information about data categories, see “Work with Data Categories” in the Salesforce online help.
In the following example, the describeDataCategoryGroupSample method returns all the category groups associated with the Article and Question objects. The describeDataCategoryGroupStructures method returns all the categories available for articles and questions in the Regions category group. For additional information about articles and questions, see “Work with Articles and Translations” in the Salesforce online help.
To use the following example, you must:
Enable Salesforce Knowledge.
Enable the answers feature.
Create a data category group called Regions.
Assign Regions as the data category group to be used by Answers.
Make sure the Regions data category group is assigned to Salesforce Knowledge.
For more information on creating data category groups, see “Create and Modify Category Groups” in the Salesforce online help. For more information on answers, see “Answers Overview” in the Salesforce online help.
1public with sharing class DescribeDataCategoryGroupSample{2 public static List<DescribeDataCategoryGroupResult>describeDataCategoryGroupSample(){3 List<DescribeDataCategoryGroupResult>describeCategoryResult;4 try{5 //Creating the list of sobjects to use for the describe6 //call7 List<String>objType = new List<String>();89 objType.add('KnowledgeArticleVersion');10 objType.add('Question');1112 //Describe Call13 describeCategoryResult = Schema.describeDataCategoryGroups(objType);1415 //Using the results and retrieving the information16 for(DescribeDataCategoryGroupResult singleResult : describeCategoryResult){17 //Getting the name of the category18 singleResult.getName();1920 //Getting the name of label21 singleResult.getLabel();2223 //Getting description24 singleResult.getDescription();2526 //Getting the sobject27 singleResult.getSobject();28}29}catch(Exception e){30}3132 return describeCategoryResult;33}34}
1public with sharing class DescribeDataCategoryGroupStructures{2 public static List<DescribeDataCategoryGroupStructureResult>3 getDescribeDataCategoryGroupStructureResults(){4 List<DescribeDataCategoryGroupResult>describeCategoryResult;5 List<DescribeDataCategoryGroupStructureResult>describeCategoryStructureResult;6 try{7 //Making the call to the describeDataCategoryGroups to8 //get the list of category groups associated9 List<String>objType = new List<String>();10 objType.add('KnowledgeArticleVersion');11 objType.add('Question');12 describeCategoryResult = Schema.describeDataCategoryGroups(objType);1314 //Creating a list of pair objects to use as a parameter15 //for the describe call16 List<DataCategoryGroupSobjectTypePair>pairs =17 new List<DataCategoryGroupSobjectTypePair>();1819 //Looping throught the first describe result to create20 //the list of pairs for the second describe call21 for(DescribeDataCategoryGroupResult singleResult :22 describeCategoryResult){23 DataCategoryGroupSobjectTypePair p =24 new DataCategoryGroupSobjectTypePair();25 p.setSobject(singleResult.getSobject());26 p.setDataCategoryGroupName(singleResult.getName());27 pairs.add(p);28}2930 //describeDataCategoryGroupStructures()31 describeCategoryStructureResult =32 Schema.describeDataCategoryGroupStructures(pairs, false);3334 //Getting data from the result35 for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){36 //Get name of the associated Sobject37 singleResult.getSobject();3839 //Get the name of the data category group40 singleResult.getName();4142 //Get the name of the data category group43 singleResult.getLabel();4445 //Get the description of the data category group46 singleResult.getDescription();4748 //Get the top level categories49 DataCategory[]toplevelCategories =50 singleResult.getTopCategories();5152 //Recursively get all the categories53 List<DataCategory>allCategories =54 getAllCategories(toplevelCategories);5556 for(DataCategory category : allCategories){57 //Get the name of the category58 category.getName();5960 //Get the label of the category61 category.getLabel();6263 //Get the list of sub categories in the category64 DataCategory[]childCategories =65 category.getChildCategories();66}67}68}catch(Exception e){69}70 return describeCategoryStructureResult;71}7273 private static DataCategory[]getAllCategories(DataCategory[]categories){74 if(categories.isEmpty()){75 return new DataCategory[]{};76}else{77 DataCategory[]categoriesClone = categories.clone();78 DataCategory category = categoriesClone[0];79 DataCategory[]allCategories = new DataCategory[]{category};80 categoriesClone.remove(0);81 categoriesClone.addAll(category.getChildCategories());82 allCategories.addAll(getAllCategories(categoriesClone));83 return allCategories;84}85}86}
Testing Access to All Data Categories Associated with an sObject
The following example tests the describeDataCategoryGroupSample method shown earlier. It ensures that the returned category group and associated objects are correct.
1@IsTest2private with sharing class DescribeDataCategoryGroupSampleTest{3 @IsTest4 public static void describeDataCategoryGroupSampleTest(){5 List<DescribeDataCategoryGroupResult>describeResult =6 DescribeDataCategoryGroupSample.describeDataCategoryGroupSample();78 //Assuming that you have KnowledgeArticleVersion and Questions9 //associated with only one category group 'Regions'.10 Assert.areEqual(2, describeResult.size(),11 'The results should only contain two results: ' + describeResult.size());1213 for(DescribeDataCategoryGroupResult result : describeResult){14 //Storing the results15 String name = result.getName();16 String label = result.getLabel();17 String description = result.getDescription();18 String objectNames = result.getSobject();1920 //asserting the values to make sure21 Assert.areEqual('Regions', name,22 'Incorrect name was returned: ' + name);23 Assert.areEqual('Regions of the World', label,24 'Incorrect label was returned: ' + label);25 Assert.areEqual('This is the category group for all the regions', description,26 'Incorrect description was returned: ' + description);27 Assert.isTrue(objectNames.contains('KnowledgeArticleVersion')28 || objectNames.contains('Question'),29 'Incorrect sObject was returned: ' + objectNames);30}31}32}
This example tests the describeDataCategoryGroupStructures method. It ensures that the returned category group, categories and associated objects are correct.
1@IsTest2private with sharing class DescribeDataCategoryGroupStructuresTest{3 @IsTest4 public static void getDescribeDataCategoryGroupStructureResultsTest(){5 List<Schema.DescribeDataCategoryGroupStructureResult>describeResult =6 DescribeDataCategoryGroupStructures.getDescribeDataCategoryGroupStructureResults();78 Assert.areEqual(2, describeResult.size(),9 'The results should only contain 2 results: ' + describeResult.size());1011 //Creating category info12 CategoryInfo world = new CategoryInfo('World', 'World');13 CategoryInfo asia = new CategoryInfo('Asia', 'Asia');14 CategoryInfo northAmerica = new CategoryInfo('NorthAmerica',15 'North America');16 CategoryInfo southAmerica = new CategoryInfo('SouthAmerica',17 'South America');18 CategoryInfo europe = new CategoryInfo('Europe', 'Europe');1920 List<CategoryInfo>info = new CategoryInfo[]{21 asia, northAmerica, southAmerica, europe22};2324 for(Schema.DescribeDataCategoryGroupStructureResult result : describeResult){25 String name = result.getName();26 String label = result.getLabel();27 String description = result.getDescription();28 String objectNames = result.getSobject();2930 //asserting the values to make sure31 Assert.areEqual('Regions', name,32 'Incorrect name was returned: ' + name);33 Assert.areEqual('Regions of the World', label,34 'Incorrect label was returned: ' + label);35 Assert.areEqual('This is the category group for all the regions', description,36 'Incorrect description was returned: ' + description);37 Assert.isTrue(objectNames.contains('KnowledgeArticleVersion')38 || objectNames.contains('Question'),39 'Incorrect sObject was returned: ' + objectNames);4041 DataCategory[]topLevelCategories = result.getTopCategories();42 Assert.areEqual(1, topLevelCategories.size(),43 'Incorrect number of top level categories returned: ' + topLevelCategories.size());44 Assert.areEqual(world.getLabel(), topLevelCategories[0].getLabel());45 Assert.areEqual(world.getName(), topLevelCategories[0].getName());4647 //checking if the correct children are returned48 DataCategory[]children = topLevelCategories[0].getChildCategories();49 Assert.areEqual(4, children.size(),50 'Incorrect number of children returned: ' + children.size());51 for(Integer i=0; i<children.size(); i++){52 Assert.areEqual(info[i].getLabel(), children[i].getLabel());53 Assert.areEqual(info[i].getName(), children[i].getName());54}55}56}5758 private with sharing class CategoryInfo{59 private final String name;60 private final String label;6162 private CategoryInfo(String n, String l){63 this.name = n;64 this.label = l;65}6667 public String getName(){68 return this.name;69}7071 public String getLabel(){72 return this.label;73}74}75}