Apex コントローラ拡張(extensions)の作成方法のサンプル

環境
Salesforce

構文
public コントローラ拡張クラス名(ApexPages.StandardController stdController) {処理コード}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
コントローラ拡張は、ApexPages.StandardController または CustomControllerName 型の単一の引数を取るコンストラクタが含まれる Apex クラスです。CustomControllerName は、拡張するカスタムコントローラの名前です。
コントローラ拡張は、ApexPages.StandardController または CustomControllerName 型の単一の引数を取るコンストラクタが含まれる Apex クラスです。CustomControllerName は、拡張するカスタムコントローラの名前です。
コントローラ拡張は、ApexPages.StandardController または CustomControllerName 型の単一の引数を取るコンストラクタが含まれる Apex クラスです。CustomControllerName は、拡張するカスタムコントローラの名前です。

1.コントローラ拡張クラスの定義
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class myControllerExtension {
private final Account acct;
    // 拡張コンストラクターはプライベートメンバーを初期化します
// 標準の getRecord メソッドを使用して変数 acct
// コントローラー
public myControllerExtension(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}
public String getGetinfo() {
return 'Welcome ' + acct.name + ' (' + acct.id + ')';
}
}
public class myControllerExtension { private final Account acct;     // 拡張コンストラクターはプライベートメンバーを初期化します // 標準の getRecord メソッドを使用して変数 acct // コントローラー public myControllerExtension(ApexPages.StandardController stdController) { this.acct = (Account)stdController.getRecord(); } public String getGetinfo() { return 'Welcome ' + acct.name + ' (' + acct.id + ')'; } }
public class myControllerExtension {

    private final Account acct;
    
    // 拡張コンストラクターはプライベートメンバーを初期化します
     // 標準の getRecord メソッドを使用して変数 acct
     // コントローラー
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }

    public String getGetinfo() {
        return 'Welcome ' + acct.name + ' (' + acct.id + ')';
    }
}

2.コントローラ拡張をページ内容で使用方法
構文1
ページマークアップで {! } 表記を使用して、コントローラ拡張メソッドを参照できます。
ページ上部の {!getinfo} 式は、コントローラ拡張の getGetinfo メソッドを参照しています。

構文2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apex:inputField value="{!account.name}"
apex:inputField value="{!account.name}"
apex:inputField value="{!account.name}"

取引先の名前を取得します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apex:commandButton value-"Save" action="{!save}"
apex:commandButton value-"Save" action="{!save}"
apex:commandButton value-"Save" action="{!save}"

action 属性のある標準 Account の save メソッドを参照します。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<apex:page standardController="Account" extensions="myControllerExtension">
{!getinfo} <p/>
<apex:form>
<apex:inputField value="{!account.name}"/> <p/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>
<apex:page standardController="Account" extensions="myControllerExtension"> {!getinfo} <p/> <apex:form> <apex:inputField value="{!account.name}"/> <p/> <apex:commandButton value="Save" action="{!save}"/> </apex:form> </apex:page>
<apex:page standardController="Account" extensions="myControllerExtension">
    {!getinfo} <p/>
    <apex:form>
        <apex:inputField value="{!account.name}"/> <p/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

 

Apex

Posted by arkgame