Newer Version Available

This content describes an older version of this product. View Latest

Accessing All Data Categories Associated with an sObject

Use the describeDataCategoryGroups and describeDataCategoryGroupStructures methods to return the categories associated with a specific object:

  1. Return all the category groups associated with the objects of your choice (see describeDataCategoryGroups(sObjectNames)).
  2. From the returned map, get the category group name and sObject name you want to further interrogate (see DescribeDataCategoryGroupResult Class).
  3. 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 class DescribeDataCategoryGroupSample {
2   public static List<DescribeDataCategoryGroupResult> describeDataCategoryGroupSample(){
3      List<DescribeDataCategoryGroupResult> describeCategoryResult;
4      try {
5         //Creating the list of sobjects to use for the describe
6         //call
7         List<String> objType = new List<String>();
8
9         objType.add('KnowledgeArticleVersion');
10         objType.add('Question');
11
12         //Describe Call
13         describeCategoryResult = Schema.describeDataCategoryGroups(objType);
14   
15         //Using the results and retrieving the information
16         for(DescribeDataCategoryGroupResult singleResult : describeCategoryResult){
17            //Getting the name of the category
18            singleResult.getName();
19
20            //Getting the name of label
21            singleResult.getLabel();
22
23            //Getting description
24            singleResult.getDescription();
25
26            //Getting the sobject
27            singleResult.getSobject();
28         }         
29      } catch(Exception e){
30      }
31      
32      return describeCategoryResult;
33   }
34}
1public 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 to
8         //get the list of category groups associated
9         List<String> objType = new List<String>();
10         objType.add('KnowledgeArticleVersion');
11         objType.add('Question');
12         describeCategoryResult = Schema.describeDataCategoryGroups(objType);
13         
14         //Creating a list of pair objects to use as a parameter
15         //for the describe call
16         List<DataCategoryGroupSobjectTypePair> pairs = 
17            new List<DataCategoryGroupSobjectTypePair>();
18         
19         //Looping throught the first describe result to create
20         //the list of pairs for the second describe call
21         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         }
29         
30         //describeDataCategoryGroupStructures()
31         describeCategoryStructureResult = 
32            Schema.describeDataCategoryGroupStructures(pairs, false);
33
34         //Getting data from the result
35         for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){
36            //Get name of the associated Sobject
37            singleResult.getSobject();
38
39            //Get the name of the data category group
40            singleResult.getName();
41
42            //Get the name of the data category group
43            singleResult.getLabel();
44
45            //Get the description of the data category group
46            singleResult.getDescription();
47
48            //Get the top level categories
49            DataCategory [] toplevelCategories = 
50               singleResult.getTopCategories();
51            
52            //Recursively get all the categories
53            List<DataCategory> allCategories = 
54               getAllCategories(toplevelCategories);
55
56            for(DataCategory category : allCategories) {
57               //Get the name of the category
58               category.getName();
59
60               //Get the label of the category
61               category.getLabel();
62
63               //Get the list of sub categories in the category
64               DataCategory [] childCategories = 
65                  category.getChildCategories();
66            }
67         }
68      } catch (Exception e){
69      }
70      return describeCategoryStructureResult;
71    }
72    
73   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@isTest
2private class DescribeDataCategoryGroupSampleTest {
3   public static testMethod void describeDataCategoryGroupSampleTest(){
4      List<DescribeDataCategoryGroupResult>describeResult =
5                 DescribeDataCategoryGroupSample.describeDataCategoryGroupSample();
6      
7      //Assuming that you have KnowledgeArticleVersion and Questions
8      //associated with only one category group 'Regions'.
9      System.assert(describeResult.size() == 2,
10           'The results should only contain two results: ' + describeResult.size());
11      
12      for(DescribeDataCategoryGroupResult result : describeResult) {
13         //Storing the results
14         String name = result.getName();
15         String label = result.getLabel();
16         String description = result.getDescription();
17         String objectNames = result.getSobject();
18         
19         //asserting the values to make sure
20         System.assert(name == 'Regions',
21         'Incorrect name was returned: ' + name);
22         System.assert(label == 'Regions of the World',
23         'Incorrect label was returned: ' + label);
24         System.assert(description == 'This is the category group for all the regions',
25         'Incorrect description was returned: ' + description);
26         System.assert(objectNames.contains('KnowledgeArticleVersion') 
27                       || objectNames.contains('Question'),
28                       'Incorrect sObject was returned: ' + objectNames);
29      }
30   }
31}

This example tests the describeDataCategoryGroupStructures method. It ensures that the returned category group, categories and associated objects are correct.

1@isTest
2private class DescribeDataCategoryGroupStructuresTest {
3   public static testMethod void getDescribeDataCategoryGroupStructureResultsTest(){
4      List<Schema.DescribeDataCategoryGroupStructureResult> describeResult =
5         DescribeDataCategoryGroupStructures.getDescribeDataCategoryGroupStructureResults();
6      
7      System.assert(describeResult.size() == 2,
8            'The results should only contain 2 results: ' + describeResult.size());
9            
10      //Creating category info
11      CategoryInfo world = new CategoryInfo('World', 'World');
12      CategoryInfo asia = new CategoryInfo('Asia', 'Asia');
13      CategoryInfo northAmerica = new CategoryInfo('NorthAmerica',
14                                                  'North America');
15      CategoryInfo southAmerica = new CategoryInfo('SouthAmerica',
16                                                  'South America');
17      CategoryInfo europe = new CategoryInfo('Europe', 'Europe');
18      
19      List<CategoryInfo> info = new CategoryInfo[] {
20        asia, northAmerica, southAmerica, europe
21     };
22      
23      for (Schema.DescribeDataCategoryGroupStructureResult result : describeResult) {
24         String name = result.getName();
25         String label = result.getLabel();
26         String description = result.getDescription();
27         String objectNames = result.getSobject();
28         
29         //asserting the values to make sure
30         System.assert(name == 'Regions', 
31         'Incorrect name was returned: ' + name);
32         System.assert(label == 'Regions of the World',
33         'Incorrect label was returned: ' + label);
34         System.assert(description == 'This is the category group for all the regions',
35         'Incorrect description was returned: ' + description);
36         System.assert(objectNames.contains('KnowledgeArticleVersion') 
37                    || objectNames.contains('Question'),
38                       'Incorrect sObject was returned: ' + objectNames);
39         
40         DataCategory [] topLevelCategories = result.getTopCategories();
41         System.assert(topLevelCategories.size() == 1,
42         'Incorrect number of top level categories returned: ' + topLevelCategories.size());
43         System.assert(topLevelCategories[0].getLabel() == world.getLabel() &&
44                       topLevelCategories[0].getName() == world.getName());
45         
46         //checking if the correct children are returned
47         DataCategory [] children = topLevelCategories[0].getChildCategories();
48         System.assert(children.size() == 4,
49         'Incorrect number of children returned: ' + children.size());
50         for(Integer i=0; i < children.size(); i++){
51            System.assert(children[i].getLabel() == info[i].getLabel() &&
52                          children[i].getName() == info[i].getName());
53         }
54      }
55      
56   }
57   
58   private class CategoryInfo {      
59      private final String name;
60      private final String label;
61            
62      private CategoryInfo(String n, String l){
63         this.name = n;
64         this.label = l;
65      }
66      
67      public String getName(){
68         return this.name;
69      }
70      
71      public String getLabel(){
72         return this.label;
73      }
74   }
75}