リストビューの取得
リストビューの取得はセットコントローラーから取得できます。
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
}
);
}