LoginSignup
7
6

More than 5 years have passed since last update.

standardControllerで取得したレコードに値をセットする

Posted at

スクリーンショット 2014-07-24 21.59.29.png

stdControllerのgetRecord()を利用すればオブジェクトの情報を簡単に取得することができます。このgetRecord()で取得できる情報はsObject型です。

なので.put()を利用することで値をセットすることができます。初期値をセットしたいときや画面に表示していない項目に値をセットしたい場合はこれで対応できます。

ContractEdit.page
<apex:page standardController="Contract" extensions="ContractEditController">
  <apex:form>
    <apex:pageBlock>
      <apex:pageBlockButtons location="bottom" id="button">
        <apex:commandButton value="保存" action="{!Save}" />
        <apex:commandButton value="キャンセル" action="{!Cancel}" />
      </apex:pageBlockButtons>
      <apex:pageBlockSection>
        <apex:inputField value="{!Contract.StartDate}" />
        <apex:inputField value="{!Contract.AccountId}" />
        <apex:inputField value="{!Contract.ContractTerm}" />
        <apex:inputField value="{!Contract.OwnerExpirationNotice}" />
      </apex:pageBlockSection>
    </apex:pageBlock>      
  </apex:form>
</apex:page>
ContractEditController
public with sharing class ContractEditController {

  public ContractEditController(ApexPages.StandardController stdController) {
    sObject sObj = stdController.getRecord();
    sObj.put('Status', 'Draft');
    sObj.put('StartDate', System.today());
    sObj.put('ContractTerm', 1);
  }
}
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6