Salesforce LWC试图使用Docs& Apex方法数据返回空白对象

发布于 2025-02-04 20:05:32 字数 3053 浏览 4 评论 0 原文

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: [{}]

谢谢!

The lightning-datatable is showing related info to the contact record.

I get the field that changed, with the new data, and the row id, but not the record Id.

Debug logs show I get back a blank Object when trying to add the recordId as per the Doc for editing multiple rows with Apex.

Everything I've googled makes it seem like some magic behind the scenes, but nothing I have tried can get this recordId added correctly. I can see the 'row' has data, but I get back a blank or empty object.

What am I missing?

html snippet

<template if:true={isContact}>
    <lightning-datatable
        key-field="id"
        data={family}
        columns={petColumns}
        onsave={handleSave}
        draft-values={draftValues}>
    </lightning-datatable>
</template>

js snippet

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')
        }
    }

Have also tried a method that uses .slice() but the same thing happens, I get a blank object back.

const recordInputs =  event.detail.draftValues.slice().map(draft => {
    const fields = Object.assign({}, draft);
    return { fields };
});

Debug shows:

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: [{}]

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命比纸薄 2025-02-11 20:05:32

您已经在标记中对钥匙场进行了较小的情况。尝试将其像钥匙线=“ 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.

仅冇旳回忆 2025-02-11 20:05:32

就像@aksa所说的那样。在组件的.html中,您应该将钥匙场设置为“ ID”,而不是“ ID”。这

<template if:true={isContact}>
    <lightning-datatable
        key-field="Id"
        data={family}
        columns={petColumns}
        onsave={handleSave}
        draft-values={draftValues}>
    </lightning-datatable>
</template>

like @Aksa stated. In the the .html of your component you should set key-field to "Id" instead of "id". The

<template if:true={isContact}>
    <lightning-datatable
        key-field="Id"
        data={family}
        columns={petColumns}
        onsave={handleSave}
        draft-values={draftValues}>
    </lightning-datatable>
</template>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文