Salesforce LWC试图使用Docs& Apex方法数据返回空白对象
Lightning-dataitable正在向联系记录显示相关信息。
我获得了更改的字段,具有新数据和行ID,但没有记录ID。
debug logs show尝试按照用于用顶点编辑多个行的文档。
我搜索过的所有内容都使它看起来像是幕后的魔术,但是我尝试过的任何内容都无法正确添加此recordID。我可以看到“行”有数据,但是我会恢复一个空白或空对象。
我想念什么?
HTML摘要
<template if:true={isContact}>
<lightning-datatable
key-field="id"
data={family}
columns={petColumns}
onsave={handleSave}
draft-values={draftValues}>
</lightning-datatable>
</template>
JS片段
const COLS = [
{
label: 'Name',
fieldName: 'recordUrl',
type: 'url',
typeAttributes: {label: { fieldName: 'Name' },
target: '_blank'},
sortable: true
},
{ label: 'Birthday', fieldName: 'Birthday__c', editable: true, type: 'date-local' },
{ label: 'Type', fieldName: 'Type__c'},
{ label: 'Age', fieldName: 'Age__c', type: 'number', cellAttributes: { alignment: 'left' } }
];
async handleSave(event) {
const updatedFields = event.detail.draftValues;
console.log('updatedFields: ' + JSON.stringify(updatedFields))
// Prepare the record IDs for getRecordNotifyChange()
const notifyChangeIds = updatedFields.map(row => {
console.log('row: ' + JSON.stringify(row))
return { "recordId": row.Id } });
// now the object is blank
console.log('notifyChangeIds: ' + JSON.stringify(notifyChangeIds))
try {
// Pass edited fields to the updatePets Apex controller
const result = await updateRecord({data: updatedFields});
console.log(JSON.stringify("Apex update result: "+ result));
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Record updated',
variant: 'success'
})
);
// Refresh LDS cache and wires
getRecordNotifyChange(notifyChangeIds);
// Display fresh data in the datatable
refreshApex(this.pets).then(() => {
// Clear all draft values in the datatable
this.draftValues = [];
});
} catch(error) {
showToast('Error updating or refreshing records', error.body.message, 'error', 'sticky')
}
}
还尝试了一种使用.slice()的方法,但同样的事情发生了,我会收回一个空白对象。
const recordInputs = event.detail.draftValues.slice().map(draft => {
const fields = Object.assign({}, draft);
return { fields };
});
调试显示:
updatedFields: [{"Birthday__c":"2001-06-14","id":"row-0"}]
myTest.js:1 row: {"Birthday__c":"2001-06-14","id":"row-0"}
myTest.js:1 notifyChangeIds: [{}]
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经在标记中对钥匙场进行了较小的情况。尝试将其像钥匙线=“ id”。我是资本。可能是API名称问题。希望它能解决。
You have put in small case in your markup against key-field. try putting it like key-field="Id". I as capital. could be an api name issue. Hopefully it will resolve.
就像@aksa所说的那样。在组件的.html中,您应该将钥匙场设置为“ ID”,而不是“ ID”。这
like @Aksa stated. In the the .html of your component you should set key-field to "Id" instead of "id". The