Apex リストビューを使ってレコードを取得するサンプル

環境
Apex

1.リストビューの取得
リストビューを取得するには ApexPages.StandardSetController を使います。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(
Database.getQueryLocator([SELECT Id FROM Product2 LIMIT 1])
);
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController( Database.getQueryLocator([SELECT Id FROM Product2 LIMIT 1]) );
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(
Database.getQueryLocator([SELECT Id FROM Product2 LIMIT 1])
);

クエリロケータによって返されるオブジェクトのリストの ApexPages.StandardSetController クラスのインスタンスを作成します。

2.このインスタンスで getListViewOptions を呼ぶことでそのユーザが参照できるリストビューを取得することができます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
List<SelectOption> listViewOptions = stdSetController.getListViewOptions();
List<SelectOption> listViewOptions = stdSetController.getListViewOptions();
List<SelectOption> listViewOptions = stdSetController.getListViewOptions();

メソッドの定義例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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;
}
@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; }
@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を表示する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
<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側実装コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@track
productListView = [];
@wire(getListView)
wiredProductListView({error, data}) {
if (data) {
this.productListView = data.map(rec => {
return {
label: rec.label,
value: rec.id
};
});
}
}
@track productListView = []; @wire(getListView) wiredProductListView({error, data}) { if (data) { this.productListView = data.map(rec => { return { label: rec.label, value: rec.id }; }); } }
@track
productListView = [];

@wire(getListView)
wiredProductListView({error, data}) {
  if (data) {
    this.productListView = data.map(rec => {
      return {
        label: rec.label,
        value: rec.id
      };
    });
  }
}

 

IT

Posted by arkgame