Apex リストビューを使ってレコードを取得するサンプル
環境
Apex
1.リストビューの取得
リストビューを取得するには ApexPages.StandardSetController を使います。
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController( Database.getQueryLocator([SELECT Id FROM Product2 LIMIT 1]) );
クエリロケータによって返されるオブジェクトのリストの ApexPages.StandardSetController クラスのインスタンスを作成します。
2.このインスタンスで getListViewOptions を呼ぶことでそのユーザが参照できるリストビューを取得することができます。
List<SelectOption> listViewOptions = stdSetController.getListViewOptions();
メソッドの定義例
@AuraEnabled(cacheable=true)
public static List<Map<String, String>> getListView() {
List<Map<String, String>> result = new List<Map<String, String>>();
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(
Database.getQueryLocator([SELECT Id FROM Product2 LIMIT 1])
);
List<SelectOption> listViewOptions = stdSetController.getListViewOptions();
for (SelectOption option: listViewOptions) {
String filterId = option.getValue();
String filterLabel = option.getLabel();
Map<String, String> m = new Map<String, String>();
m.put('id', filterId);
m.put('label', filterLabel);
result.add(m);
}
return result;
}
3.Lightning Web Component側(LWC側)にlightning-comboboxを表示する
<template>
<div class="c-container">
<lightning-combobox
name="products"
label="商品概要"
placeholder="Select Listview"
options={productListView}
onchange={handleListview}
></lightning-combobox>
<div class="cft"></div>
</div>
</template>
4.JavaScript側実装コード
@track
productListView = [];
@wire(getListView)
wiredProductListView({error, data}) {
if (data) {
this.productListView = data.map(rec => {
return {
label: rec.label,
value: rec.id
};
});
}
}