我在 Salesforce 的记录详细信息页面上公开了一个自定义 LWC 组件。该组件包括一个动态查询选项列表值的 Design 属性。当我将此组件拖放到应用程序生成器页面上并单击“保存”时。单击“保存”后,会生成一个用户日志,显示动态选项列表查询被执行了两次。我不确定单击“保存”时双重执行查询背后的原因。
JS代码:
import { LightningElement, api } from 'lwc';
export default class ListView extends LightningElement {
@api AccountName;
}
HTML代码:
<template>
test
</template>
XML 文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
<masterLabel>List View</masterLabel>
<targetConfigs>
<targetConfig targets="lightning__HomePage,lightning__RecordPage,lightning__AppPage,lightning__UtilityBar">
<property
name="AccountName"
default=""
required="true"
type="String"
datasource="apex://AccountNamePicklist"
description="Name of the List view configuration. This value is required."
label="Configuration Name"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Apex 类代码:
public with sharing class AccountNamePicklist extends VisualEditor.DynamicPickList {
List<Account> AccountList = new List<Account>();
public AccountNamePicklist() {
System.debug('enter Method');
/*---------------------- This query executed Two times ---------------------*/
AccountList = [SELECT Id, Name FROM Account WITH USER_MODE ORDER BY CreatedDate DESC LIMIT 2000];
}
public override VisualEditor.DataRow getDefaultValue() {
VisualEditor.DataRow defaultValue = new VisualEditor.DataRow('', '');
if (AccountList.size() > 0) {
defaultValue = new VisualEditor.DataRow(AccountList[0].Name, AccountList[0].Name);
}
return defaultValue;
}
public override VisualEditor.DynamicPickListRows getValues() {
VisualEditor.DynamicPickListRows AccountNameList = new VisualEditor.DynamicPickListRows();
for (Account acc : AccountList) {
VisualEditor.DataRow value = new VisualEditor.DataRow(acc.Name, acc.Name);
AccountNameList.addRow(value);
}
return AccountNameList;
}
}
}
Salesforce 文档显示此文本:
VisualEditor.DynamicPickList 方法 isValid() 在 Experience 中运行 页面加载时的云站点。如果你不重写该方法 您的自定义 Apex 类扩展了 VisualEditor.DynamicPickList, 默认实现在运行时执行 getValues(),这可以 导致性能下降。为了提高性能,实施 非操作 isValid()
这会导致 getValues() 方法被执行多次,因为它还用于检查所选值是否是有效值,这也可能意味着该类被实例化两次。
为了避免这种行为,您可以重写 isValid() 函数并仅在需要帐户时才执行查询:
public override Boolean isValid(Object attributeValue) {
return true;
}
还删除执行查询的构造函数,并将 accountList 替换为以下内容:
List<Account> accountList {
get {
if (accountList == null) {
initAccountList();
}
return accountList;
}
set;
}
private static List<Account> initAccountList() {
this.accountList = [SELECT Id, Name FROM Account WITH USER_MODE ORDER BY CreatedDate DESC LIMIT 2000];
}
方法。https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_config_for_app_builder_dynamic_picklists.htm