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

関連リストの使用例

動的 Visualforce コンポーネントの使用は、参照するオブジェクトの種別がわからない場合に最適です。一方、動的 Visualforce バインドの使用は、アクセスする項目がわからない場合に最適です。

動的 Visualforce を使用するための次のシナリオ���は、アクセスする必要がある既知の項目セットを使用して単純で再利用可能なページを構築します。ページとそのカスタムオブジェクトは、未管理パッケージに配置され、同じ組織全体に配布されます。

最初に、Classroom という名前のカスタムオブジェクトを作成します。次の図のように、2 つのオブジェクトを作成し、それぞれに Science 101 および Math 201 という名前を付けます。
最近追加された Classroom の画像
次に、さらに 2 つのカスタムオブジェクトを作成して、それぞれに Student および Teacher という名前を付けます。各オブジェクトの作成が完了したら、次の手順を実行します。
  1. [カスタム項目 & リレーション] の下にある [新規] をクリックします。
  2. [主従関係] を選択し、[次へ] をクリックします。
  3. ドロップダウンリストで [教室] を選択し、[次へ] をクリックします。
  4. 続いて [次へ] をクリックし、すべてのデフォルト値を変更せずに残します。
次のオブジェクトと照合リレーションを作成します。
  • Johnny Walker という名前の新規 Student と Mister Pibb という名前の新規 Teacher の両方を Science 101 に割り当てます。
  • 別の Boont Amber という名前の新規 Student と Doctor Pepper という名前の Teacher の両方を Math 201 に割り当てます。
次に、DynamicClassroomList という名前で新規 Apex ページを作成し、次のコードを貼り付けます。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class DynamicClassroomList {
18
19    private ApexPages.StandardSetController controller;
20    private PageReference savePage;
21    private Set<String> unSelectedNames;
22    private Set<String> selectedNames;
23    
24    public List<String> selected { get; set; }
25    public List<String> unselected { get; set; }
26    public String objId { get; set; }
27    public List<String> displayObjs {
28        get; private set;
29    }
30    
31    boolean idIsSet = false;
32    
33    public DynamicClassroomList() {
34        init();
35    }
36
37    public DynamicClassroomList(ApexPages.StandardSetController con) {
38        this.controller = con;
39        init();
40    }
41
42    private void init() {
43        savePage = null;
44        unSelectedNames = new Set<String>();
45        selectedNames = new Set<String>();
46         
47        if (idIsSet) {
48            ApexPages.CurrentPage().getParameters().put('id', objId);
49            idIsSet = false;
50        }
51    }
52    
53    public PageReference show() {
54        savePage = Page.dynVFClassroom;
55        savePage.getParameters().put('id', objId);
56        return savePage;
57    }
58
59    public List<SelectOption> displayObjsList { 
60        get {
61            List<SelectOption> options = new List<SelectOption>();
62            List<Classroom__c> classrooms = [SELECT id, name FROM Classroom__c];
63            
64            for (Classroom__c c: classrooms) {
65                options.add(new SelectOption(c.id, c.name)); 
66            }
67            
68            return options;
69       }
70    }
71    
72    public PageReference customize() {
73        savePage = ApexPages.CurrentPage();
74        savePage.getParameters().put('id', objId);
75        
76        return Page.dynamicclassroomlist;
77    }
78
79    // The methods below are for constructing the select list  
80
81    public List<SelectOption> selectedOptions { 
82        get {
83            List<String> sorted = new List<String>(selectedNames);
84            sorted.sort();
85            List<SelectOption> options = new List<SelectOption>();
86            for (String s: sorted) {
87                options.add(new SelectOption(s, s));
88            }
89            return options;
90        }
91    }
92    
93    public List<SelectOption> unSelectedOptions { 
94        get {
95            Schema.DescribeSObjectResult R = Classroom__c.SObjectType.getDescribe();
96            List<Schema.ChildRelationship> C = R.getChildRelationships(); 
97            List<SelectOption> options = new List<SelectOption>();
98            
99            for (Schema.ChildRelationship cr: C) {
100                String relName = cr.getRelationshipName();
101                // We're only interested in custom relationships
102                if (relName != null && relName.contains('__r')) {
103                    options.add(new SelectOption(relName, relName));
104                }
105            }
106            return options;
107        }
108    }
109
110
111    public void doSelect() {
112        for (String s: selected) {
113            selectedNames.add(s);
114            unselectedNames.remove(s);
115        }
116    }
117
118    public void doUnSelect() {
119        for (String s: unselected) {
120            unSelectedNames.add(s);
121            selectedNames.remove(s);
122        }
123    }
124
125    public Component.Apex.OutputPanel getClassroomRelatedLists() {
126        Component.Apex.OutputPanel dynOutPanel= new Component.Apex.OutputPanel();
127        
128        for(String id: selectedNames) {
129           Component.Apex.RelatedList dynRelList = new Component.Apex.RelatedList();
130           dynRelList.list = id;
131           dynOutPanel.childComponents.add(dynRelList);
132        }
133        
134        return dynOutPanel;
135    }
136}
保存しようとすると、Visualforce ページが存在しないというメッセージが表示される場合があります。リンクをクリックしてページを作成すると、その後にあるコードブロックが入力されます。
次に、dynVFClassroom という名前で Visualforce ページを作成し、次のコードを貼り付けます。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page standardController="Classroom__c" recordSetVar="classlist"
18    extensions="DynamicClassroomList">
19    
20    <apex:dynamicComponent componentValue="{!ClassroomRelatedLists}"/>
21    
22    <apex:form>
23        
24        <apex:pageBlock title="Classrooms Available" mode="edit">
25            <apex:pageMessages/>
26            <apex:selectRadio value="{!objId}">
27                <apex:selectOptions value="{!displayObjsList}"/>
28            </apex:selectRadio>
29        </apex:pageBlock>
30
31        <apex:commandButton value="Select Related Items" action="{!Customize}"/>
32    </apex:form>
33    
34</apex:page>
最後に、DynamicClassroomList という名前のページを作成します。このチュートリアルを最初から実行している場合は、コントローラ拡張を構築したときにこのページをすでに作成しているはずです。次のコードを貼り付けます。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page standardController="Classroom__c" recordsetvar="listPageMarker" 
18    extensions="DynamicClassroomList">
19    <apex:messages/><br/>
20    <apex:form>
21        <apex:pageBlock title="Select Relationships to Display" id="selectionBlock">
22            <apex:panelGrid columns="3">
23                <apex:selectList id="unselected_list" required="false"
24                    value="{!selected}" multiselect="true" size="20" 
25                    style="width:250px">
26                    <apex:selectOptions value="{!unSelectedOptions}"/>
27                </apex:selectList>
28                <apex:panelGroup>
29                    <apex:commandButton value=">>" action="{!DoSelect}" 
30                        reRender="selectionBlock"/>
31                    <br/>
32                    <apex:commandButton value="<<" action="{!DoUnselect}" 
33                        reRender="selectionBlock"/>
34                </apex:panelGroup>
35                <apex:selectList id="selected_list" required="false"
36                    value="{!unselected}" multiselect="true" size="20" 
37                    style="width:250px">
38                    <apex:selectOptions value="{!selectedOptions}"/>
39                </apex:selectList>
40            </apex:panelGrid>
41        </apex:pageBlock>
42        <br/>
43        <apex:commandButton value="Show Related Lists" action="{!show}"/>
44    </apex:form>
45</apex:page>
これは、表示するオブジェクトリレーションを選択するオプションをユーザに表示するページです。「selected」リストと「unselected」リストへの入力は、動的な方法で行われます。

コントローラ拡張とこれらのページを作成したら、組織の /apex/dynVFClassroom に移動します。次のようなシーケンスが表示されます。初期 Classroom オブジェクト選択のスクリーンショットClassroom リレーション選択のスクリーンショットClassroom リレーション選択のスクリーンショット