この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

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

sObject に関連付けられたすべてのデータカテゴリへのアクセス

describeDataCategoryGroups メソッドおよび describeDataCategoryGroupStructures メソッドを使用して、特定のオブジェクトに関連付けられたカテゴリを返します。

  1. 選択したオブジェクトに関連付けられたすべてのカテゴリグループを返します (describeDataCategoryGroups(sObjectNames)を参照)。
  2. 返された対応付けから、詳細に検索するカテゴリグループ名と sObject 名を取得します (Describe​DataCategoryGroupResult クラスを参照)。
  3. カテゴリグループおよび関連付けられたオブジェクトを指定し、このオブジェクトに使用できるカテゴリを取得します (describeDataCategoryGroupStructuresを参照)。

describeDataCategoryGroupStructures メソッドは、指定したカテゴリグループのオブジェクトに使用できるカテゴリを返します。データカテゴリについての詳細は、Salesforce オンラインヘルプの「データカテゴリとは?」を参照してください。

次の例では、describeDataCategoryGroupSample メソッドは、Article オブジェクトおよび Question オブジェクトに関連付けられたすべてのカテゴリグループを返します。describeDataCategoryGroupStructures メソッドは、領域カテゴリグループの記事および質問に使用できるすべてのカテゴリを返します。記事および質問についての詳細は、Salesforce オンラインヘルプの「記事と翻訳の管理」および「アンサーの概要」を参照してください。

次の例を使用するには、次を行う必要があります。

  • Salesforce ナレッジを有効化する。
  • アンサー機能を有効化する。
  • 領域というデータカテゴリグループを作成する。
  • 領域をアンサーで使用するデータカテゴリグループとして割り当てる。
  • 領域データカテゴリグループが Salesforce ナレッジに割り当てられていることを確認する。

データカテゴリグループの作成についての詳細は、Salesforce オンラインヘルプの「カテゴリグループの作成と変更」を参照してください。アンサーについての詳細は、Salesforce オンラインヘルプの「アンサーの概要」を参照してください。

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
52
1swfobject.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
104

sObject に関連付けられたすべてのデータカテゴリへのアクセスのテスト

次の例では、上記の describeDataCategoryGroupSample メソッドをテストします。返されたカテゴリグループおよび関連付けられたオブジェクトが正しいことを確認できます。

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}

この例では、describeDataCategoryGroupStructures メソッドをテストします。返されたカテゴリグループ、カテゴリ、および関連付けられたオブジェクトが正しいことを確認できます。

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}