1
2

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.

【salesforce】ごちゃごちゃしたリストの表示はMapよりも内部クラスの方が楽

Posted at

オブジェクトを一つに限らず、二つ三つ跨いで取得してVF側で表示させる際に

Mapを使ってネストさせるのもいいんですが、

内部クラスを使うとかなり簡単に値の表示ができます。

    public class wrapperClass{
        
        public Integer i { get; set; }
        public String s { get; set; }
        public Contact c { get; set; }
        
        public wrapperClass(){
            this.i = 1;
            this.s = 'Sample';
            this.c = new Contact(
              FirstName = 'Test'
            );
        }
    }

書き方はシンプルで普通にクラスとコンストラクタを用意します。

親のクラスの方では、

  public class wrapperTestcontroller {
    
    public List<wrapperClass> wrapperList{ get;  set; }
    
    public wrapperTestcontroller(){
        this.conditions = new Contact();
        this.wrapperList = new List<wrapperClass>();
        wrapperList.add(new wrapperClass());
        system.debug(wrapperList);
    }

リストに格納してしまいましょう。
※初期化するのを忘れるとエラーで返されます。

今回はInteger,String,Contactをごちゃまぜたリストにしました。

これをデバッグでみるとどうなるでしょうか。

スクリーンショット 2019-11-12 22.22.40.png

うまく値が入っています。

これをVF側に渡してあげれば、オブジェクトなどの垣根を無視したリストが作れるので

うまく値を表示させるのに便利です。

またコンストラクタに引数を渡せばその引数を使ってSOQLを使ったりもできます。

もちろん引数を違うものを設定すれば違う処理を書くこともできます。

MapをVFで使う時ってネストされてると使いにくいイメージがあるのでこれだとかなり便利ですね。

profile

生まれも育ちも大阪の浪速中の浪速っ子が30才未経験からITエンジニアとして生きるブログもやってます。
よかったらみてください:muscle_tone2:

PVアクセスランキング にほんブログ村

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?