Apex StandardSetController クラスのサンプル

環境
Salesforce

概要
StandardSetController オブジェクトを使用すると、Salesforce が提供する、プリビルドされた Visualforce リ
ストコントローラーと同様のリストコントローラー、またはその拡張としてリストコントローラーを作成でき
ます。

使用方法
StandardSetController クラスには、プロトタイプオブジェクトも含まれます。これは、Visualforce の
StandardSetController クラスに含まれる単一の sObject です。プロトタイプオブジェクトの項目が設定されてい
る場合、それらの値は、保存操作中に使用されます。

StandardSetController オブジェクトのカスタムリストコントローラーのコンストラクターでの使用
方法を示します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class opportunityList2Con {
// ApexPages.StandardSetController をインスタンス化する
// 標準リストコントローラーの場合
public ApexPages.StandardSetController setCon {
get {
if(setCon == null) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[SELECT Name, CloseDate FROM Opportunity]));
}
return setCon;
}
set;
}
// setConを初期化し、レコードのリストを返す
public List<Opportunity> getOpportunities() {
return (List<Opportunity>) setCon.getRecords();
}
}
public class opportunityList2Con { // ApexPages.StandardSetController をインスタンス化する // 標準リストコントローラーの場合 public ApexPages.StandardSetController setCon { get { if(setCon == null) { setCon = new ApexPages.StandardSetController(Database.getQueryLocator( [SELECT Name, CloseDate FROM Opportunity])); } return setCon; } set; } // setConを初期化し、レコードのリストを返す public List<Opportunity> getOpportunities() { return (List<Opportunity>) setCon.getRecords(); } }
public class opportunityList2Con {
  // ApexPages.StandardSetController をインスタンス化する
  // 標準リストコントローラーの場合
public ApexPages.StandardSetController setCon {
  get {
     if(setCon == null) {
        setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
          [SELECT Name, CloseDate FROM Opportunity]));
       }
      return setCon;
    }
   set;
}
// setConを初期化し、レコードのリストを返す
public List<Opportunity> getOpportunities() {
  return (List<Opportunity>) setCon.getRecords();
  }
}

次の Visualforce マークアップは、上記のコントローラーをページ内で使用する方法を示します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<apex:page controller="opportunityList2Con">
<apex:pageBlock>
<apex:pageBlockTable value="{!opportunities}" var="opp">
<apex:column value="{!opp.Name}"/>
<apex:column value="{!opp.CloseDate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
<apex:page controller="opportunityList2Con"> <apex:pageBlock> <apex:pageBlockTable value="{!opportunities}" var="opp"> <apex:column value="{!opp.Name}"/> <apex:column value="{!opp.CloseDate}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
<apex:page controller="opportunityList2Con">
 <apex:pageBlock>
  <apex:pageBlockTable value="{!opportunities}" var="opp">
     <apex:column value="{!opp.Name}"/>
     <apex:column value="{!opp.CloseDate}"/>
   </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

 

Apex

Posted by arkgame