LoginSignup
4
3

More than 5 years have passed since last update.

リストビュー取得と、それに対応するレコードリストの全取得

Last updated at Posted at 2016-07-12

リストビューの取得

リストビューの取得はセットコントローラーから取得できます。
database.getQueryLocatorで取得したいオブジェクトをSOQLで設定してください。
その後、SelectOption型として取得する事で、リストビューIDリストビューラベルを取得する事ができます。

リストビューのIDとラベルを取得する
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController( 
        database.getQueryLocator( 'SELECT id FROM Account LIMIT 1' )
);
List< SelectOption > listViewOptions = stdSetController.getListViewOptions();
for ( SelectOption option : listViewOptions ){
    Id filterId = option.getValue();
    String filterLabel = option.getLabel();
    System.debug(filterId + ':' + filterLabel);
}        

リストビューと対応するレコードリストの全取得

上記で取得したリストビューを元に、以下の様な JSON でクライアントにレコードを返却する。

取得結果(例)
[{
  id: 'xxxxxxxxxxxxxxx',
  label: 'カスタムリストビュー1',
  records: [{
    id: 'xxxxxxxxxxxxxxx',
    name: 'レコード名1'
  }, {
    id: 'xxxxxxxxxxxxxxx',
    name: 'レコード名2'
  }]
}, {
  id: 'xxxxxxxxxxxxxxx',
  label: 'カスタムリストビュー2',
  records: [{
    id: 'xxxxxxxxxxxxxxx',
    name: 'レコード名3'
  }, {
    id: 'xxxxxxxxxxxxxxx',
    name: 'レコード名4'
  }]
}]
リストビューと対応するレコードリストの全取得
// 取得対象のリストビューは予め、ラベル名で指定しておく
private static Set<String> TARGET_FILTERS = new Set<String>{'カスタムリストビュー1', 
                                                            'カスタムリストビュー2'};

@RemoteAction
global static List< Map< String, Object >> getCustomerList(){
    ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController( 
        database.getQueryLocator( 'select id from Account limit 1' )
    );
    List< SelectOption > listViewOptions = stdSetController.getListViewOptions();
    List< Map< String, Object >> ret = new List< Map< String, Object >>();
    for ( SelectOption option : listViewOptions ){
        Id filterId = option.getValue();
        String filterLabel = option.getLabel();

        // 特定のリストビューにしぼりたい場合は、上記TARGET_FILTERSを編集
        if( !TARGET_FILTERS.contains(filterLabel) ) continue;

        // 扱いやすいように Map に変換する
        Map<String, Object> listViewMap = createListViewMap( filterId, filterLabel );

        // リスト毎のレコードを取得するための標準セットコントローラーを作成・設定する
        stdSetController = new ApexPages.StandardSetController( 
            database.getQueryLocator( 'select id, name from Account limit 1' ));
        stdSetController.setPageSize(200); // ページあたり200件(最大)
        stdSetController.setFilterID( filterId ); // 対象リストビューのID
        // ページループ開始
        while( true ){  

            // レコードループ開始
            for(Account rec : ( List<Account> ) stdSetController.getRecords()) {
                addRecord( listViewMap, rec.id, rec.name );
            }

            // 次のページが無ければページループを抜ける
            if( !stdSetController.getHasNext()) break;

            // 次のページで移動
            stdSetController.next();
        } 
        ret.add( listViewMap );
    }        
    return ret;
}
private static Map< String, Object > createListViewMap( Id     listViewId, 
                                                        String listViewLabel ){
    return new Map< String, Object >{ 
        'id'      => listViewId, 
        'label'   => listViewLabel, 
        'records' => new List< Map< String, String >>()
    };
}
private static void addRecord( Map<String, Object> listViewMap, 
                               Id                  recId, 
                               String              name ){
    (( List< Map< String, String >>)listViewMap.get( 'records' )).add( 
        new Map< String, String >{ 
            'id'   => recId, 
            'name' => name 
        }
    );
}
4
3
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
4
3