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

Newer Version Available

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

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

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

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

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

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

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

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

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

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}

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

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

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}

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

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}