2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VFページ コントローラ

Last updated at Posted at 2017-01-24

標準コントローラを拡張するカスタムコントローラ

<!-- standardControllerにsObject名、extensionsに拡張コントローラクラスを指定 -->
<apex:page standardController="XX_c" sidebar="false" id="page" extensions="XX_VF_Ctrl">
</apex:page>
//stdconを引数とするコンストラクタは必須 
public with sharing class XX_VF_Ctrl {
    
    XX__c crecord;
    
    /*
     * Constructor
     */ 
    public XX_VF_Ctrl(ApexPages.StandardController stdcon){
        crecord = (XX__c)stdcon.getRecord();
    }
}

VFページを返すPOSTメソッド

<apex:commandButton value="サブミット" action="{!xxxSubmit}" />

public PageReference xxxSubmit(){
    PageReference pr = Page.xxx_VFpage; //VFページの名前を指定
    ....
    return pr;
}

パラメータを取得する

GETもPOSTも受け取れる
String jsonstr = Apexpages.currentPage().getParameters().get('selectedCategories');

画面表示しない項目をコントローラ側から参照したい場合はaddFieldsを使う

public xx_Ctrl(ApexPages.StandardController stdcon) {
    //最初に実行すること (テスト時に失敗するため、画面から起動された場合のみで動くようにする)
    if (!Test.isRunningTest()) { 
        stdCon.addFields(new List<String>{'pageType__c', 'SnapshotData__c', 'approvalDate__c', 'status__c'});
    }

cookieの参照・出力

クライアント側のJSで参照する場合はCookie名の前に自動的に共通の接頭辞 'apex__' が付くことに注意

private static String COOKIE_NAME = 'myCookie';
private static Integer SESSION_COOKIE = -1;

private void setCookieValue(Integer v1, String v2){
    Map<String, Integer> data = new Map<String, Integer>();
    data.put('key1', v1);
    data.put('key2', v2);
    Cookie cvalue = new Cookie(COOKIE_NAME, JSON.serialize(data), null, SESSION_COOKIE, true);
    ApexPages.currentPage().setCookies(new Cookie[]{cvalue});
    }

private Map<String, Integer> getCookieValue(){
    Cookie cvalue = ApexPages.currentPage().getCookies().get(COOKIE_NAME);
    if (cvalue==null){
        return new Map<String, Integer>{'key1'=>0, 'key2'=>''};
    }else{
        String value = cvalue.getValue();
        return (Map<String, Integer>)JSON.deserialize(value, Map<String, Integer>.class);
    }
}

エラーの出力

//トリガー内ではオブジェクト/レコードに対して
XXX_c record;
...
this.record.addError('エラーメッセージ'); //オブジェクト全体に対するエラー
this.record.xxx_c.addError('エラーメッセージ');   //特定の項目に対するエラー

//VFコントローラ内ではフィールドまたはページに対して
try{
  ....
}catch(Exception e){
    //単にエラーメッセージを表示するケース
    ApexPages.addMessages(e);
    //sevirtyを指定
    ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,e.getMessage()));
    //ApexPages.severity.CONFIRM
    //ApexPages.severity.ERROR
    //ApexPages.severity.FATAL
    //ApexPages.severity.INFO
    //ApexPages.severity.WARNING

//エラーメッセージのクリア
ApexPages.getMessages().clear();

参考サイト

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?