LWC组合了来自不同方法的2个列表的结果



在我的LWC组件JS中,我使用@wire直接从Salesforce中的记录中查询2个字段(名称和移动(的当前值。我正在将这些值保存到列表中。

我在connectedCallback((中有另一个方法getEmpWrapperList,它查询相同的字段(名称和移动(,但它使用现有的Apex类,因为它需要从遗留系统查询这些信息&而不是直接来自Salesforce记录。

我想把这两个结果合并成一个列表——来自@wire的结果和来自getEmpWrapperList的结果。

基本上,我想将savedEmpObj和this.empWrapperList结果结合起来。但既然connectedCallback((和@wire是自调用的,我该如何实现这一点?

emp。JS

@wire(getRecord, { recordId: '$record.Id', fields: ["Object__c.Name__c","Object__c.Mobile__c"] })
empRecord({ error, data }) {
if (error) {
console.log('error in wire', error);
} else if (data) {
this.empRecordName = data.fields.Name__c.value;
this.empRecordMobile = data.fields.Mobile__c.value;
let savedEmpObj = {};
savedEmpObj.empName = this.empRecordName;
savedEmpObj.empMobile = this.empRecordMobile;
}
}
connectedCallback() {

getEmpWrapperList({ empid: this.empid, org: this.org, empcode: this.empcode })   //Apex class to fetch data from integrated system
.then(result => {
this.empWrapperList = result;    //contains Name, Mobile from integrated system
this.error = undefined;
})
.catch(error => {
this.error = error;
this.empWrapperList = undefined;
});
}

您可以尝试使用Apex类来组合这两个列表,方法是将记录连接到您的自定义Apex方法getEmpList(String-id(,而不是使用标准的getRecord函数和其他从遗留系统获取数据的方法。LWC看起来是这样的:

@track
empList;
@wire(getEmpList, { id: '$record.Id' }
wiredEmpList(value) {
this.empList = value;
if(value.error) {
this.empList.error = value.error;
} else if (value.data) {
this.empList.data = value.data;
}
}

最新更新