Newer Version Available
Accessing All Data Categories Associated with an sObject
Use the describeDataCategoryGroups and describeDataCategoryGroupStructures methods to return the categories associated with a specific object:
- Return all the category groups associated with the objects of your choice (see describeDataCategoryGroups(sObjectNames)).
- From the returned map, get the category group name and sObject name you want to further interrogate (see DescribeDataCategoryGroupResult Class).
- 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 “What are 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 “Managing Articles and Translations” and “Answers Overview” 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 “Creating and Modifying Category Groups” in the Salesforce online help. For more information on answers, see “Answers Overview” in the Salesforce online help.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class DescribeDataCategoryGroupSample {
18 public static List<DescribeDataCategoryGroupResult> describeDataCategoryGroupSample(){
19 List<DescribeDataCategoryGroupResult> describeCategoryResult;
20 try {
21 //Creating the list of sobjects to use for the describe
22 //call
23 List<String> objType = new List<String>();
24
25 objType.add('KnowledgeArticleVersion');
26 objType.add('Question');
27
28 //Describe Call
29 describeCategoryResult = Schema.describeDataCategoryGroups(objType);
30
31 //Using the results and retrieving the information
32 for(DescribeDataCategoryGroupResult singleResult : describeCategoryResult){
33 //Getting the name of the category
34 singleResult.getName();
35
36 //Getting the name of label
37 singleResult.getLabel();
38
39 //Getting description
40 singleResult.getDescription();
41
42 //Getting the sobject
43 singleResult.getSobject();
44 }
45 } catch(Exception e){
46 }
47
48 return describeCategoryResult;
49 }
50}
51
521swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class DescribeDataCategoryGroupStructures {
18 public static List<DescribeDataCategoryGroupStructureResult>
19 getDescribeDataCategoryGroupStructureResults(){
20 List<DescribeDataCategoryGroupResult> describeCategoryResult;
21 List<DescribeDataCategoryGroupStructureResult> describeCategoryStructureResult;
22 try {
23 //Making the call to the describeDataCategoryGroups to
24 //get the list of category groups associated
25 List<String> objType = new List<String>();
26 objType.add('KnowledgeArticleVersion');
27 objType.add('Question');
28 describeCategoryResult = Schema.describeDataCategoryGroups(objType);
29
30 //Creating a list of pair objects to use as a parameter
31 //for the describe call
32 List<DataCategoryGroupSobjectTypePair> pairs =
33 new List<DataCategoryGroupSobjectTypePair>();
34
35 //Looping throught the first describe result to create
36 //the list of pairs for the second describe call
37 for(DescribeDataCategoryGroupResult singleResult :
38 describeCategoryResult){
39 DataCategoryGroupSobjectTypePair p =
40 new DataCategoryGroupSobjectTypePair();
41 p.setSobject(singleResult.getSobject());
42 p.setDataCategoryGroupName(singleResult.getName());
43 pairs.add(p);
44 }
45
46 //describeDataCategoryGroupStructures()
47 describeCategoryStructureResult =
48 Schema.describeDataCategoryGroupStructures(pairs, false);
49
50 //Getting data from the result
51 for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){
52 //Get name of the associated Sobject
53 singleResult.getSobject();
54
55 //Get the name of the data category group
56 singleResult.getName();
57
58 //Get the name of the data category group
59 singleResult.getLabel();
60
61 //Get the description of the data category group
62 singleResult.getDescription();
63
64 //Get the top level categories
65 DataCategory [] toplevelCategories =
66 singleResult.getTopCategories();
67
68 //Recursively get all the categories
69 List<DataCategory> allCategories =
70 getAllCategories(toplevelCategories);
71
72 for(DataCategory category : allCategories) {
73 //Get the name of the category
74 category.getName();
75
76 //Get the label of the category
77 category.getLabel();
78
79 //Get the list of sub categories in the category
80 DataCategory [] childCategories =
81 category.getChildCategories();
82 }
83 }
84 } catch (Exception e){
85 }
86 return describeCategoryStructureResult;
87 }
88
89 private static DataCategory[] getAllCategories(DataCategory [] categories){
90 if(categories.isEmpty()){
91 return new DataCategory[]{};
92 } else {
93 DataCategory [] categoriesClone = categories.clone();
94 DataCategory category = categoriesClone[0];
95 DataCategory[] allCategories = new DataCategory[]{category};
96 categoriesClone.remove(0);
97 categoriesClone.addAll(category.getChildCategories());
98 allCategories.addAll(getAllCategories(categoriesClone));
99 return allCategories;
100 }
101 }
102}
103
104Testing 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.
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class DescribeDataCategoryGroupSampleTest {
19 public static testMethod void describeDataCategoryGroupSampleTest(){
20 List<DescribeDataCategoryGroupResult>describeResult =
21 DescribeDataCategoryGroupSample.describeDataCategoryGroupSample();
22
23 //Assuming that you have KnowledgeArticleVersion and Questions
24 //associated with only one category group 'Regions'.
25 System.assert(describeResult.size() == 2,
26 'The results should only contain two results: ' + describeResult.size());
27
28 for(DescribeDataCategoryGroupResult result : describeResult) {
29 //Storing the results
30 String name = result.getName();
31 String label = result.getLabel();
32 String description = result.getDescription();
33 String objectNames = result.getSobject();
34
35 //asserting the values to make sure
36 System.assert(name == 'Regions',
37 'Incorrect name was returned: ' + name);
38 System.assert(label == 'Regions of the World',
39 'Incorrect label was returned: ' + label);
40 System.assert(description == 'This is the category group for all the regions',
41 'Incorrect description was returned: ' + description);
42 System.assert(objectNames.contains('KnowledgeArticleVersion')
43 || objectNames.contains('Question'),
44 'Incorrect sObject was returned: ' + objectNames);
45 }
46 }
47}This example tests the describeDataCategoryGroupStructures method. It ensures that the returned category group, categories and associated objects are correct.
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class DescribeDataCategoryGroupStructuresTest {
19 public static testMethod void getDescribeDataCategoryGroupStructureResultsTest(){
20 List<Schema.DescribeDataCategoryGroupStructureResult> describeResult =
21 DescribeDataCategoryGroupStructures.getDescribeDataCategoryGroupStructureResults();
22
23 System.assert(describeResult.size() == 2,
24 'The results should only contain 2 results: ' + describeResult.size());
25
26 //Creating category info
27 CategoryInfo world = new CategoryInfo('World', 'World');
28 CategoryInfo asia = new CategoryInfo('Asia', 'Asia');
29 CategoryInfo northAmerica = new CategoryInfo('NorthAmerica',
30 'North America');
31 CategoryInfo southAmerica = new CategoryInfo('SouthAmerica',
32 'South America');
33 CategoryInfo europe = new CategoryInfo('Europe', 'Europe');
34
35 List<CategoryInfo> info = new CategoryInfo[] {
36 asia, northAmerica, southAmerica, europe
37 };
38
39 for (Schema.DescribeDataCategoryGroupStructureResult result : describeResult) {
40 String name = result.getName();
41 String label = result.getLabel();
42 String description = result.getDescription();
43 String objectNames = result.getSobject();
44
45 //asserting the values to make sure
46 System.assert(name == 'Regions',
47 'Incorrect name was returned: ' + name);
48 System.assert(label == 'Regions of the World',
49 'Incorrect label was returned: ' + label);
50 System.assert(description == 'This is the category group for all the regions',
51 'Incorrect description was returned: ' + description);
52 System.assert(objectNames.contains('KnowledgeArticleVersion')
53 || objectNames.contains('Question'),
54 'Incorrect sObject was returned: ' + objectNames);
55
56 DataCategory [] topLevelCategories = result.getTopCategories();
57 System.assert(topLevelCategories.size() == 1,
58 'Incorrect number of top level categories returned: ' + topLevelCategories.size());
59 System.assert(topLevelCategories[0].getLabel() == world.getLabel() &&
60 topLevelCategories[0].getName() == world.getName());
61
62 //checking if the correct children are returned
63 DataCategory [] children = topLevelCategories[0].getChildCategories();
64 System.assert(children.size() == 4,
65 'Incorrect number of children returned: ' + children.size());
66 for(Integer i=0; i < children.size(); i++){
67 System.assert(children[i].getLabel() == info[i].getLabel() &&
68 children[i].getName() == info[i].getName());
69 }
70 }
71
72 }
73
74 private class CategoryInfo {
75 private final String name;
76 private final String label;
77
78 private CategoryInfo(String n, String l){
79 this.name = n;
80 this.label = l;
81 }
82
83 public String getName(){
84 return this.name;
85 }
86
87 public String getLabel(){
88 return this.label;
89 }
90 }
91}