本篇参考:

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get_record_notify

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_notify_record_update

我们在Salesforce LWC学习(二十九) getRecordNotifyChange(LDS拓展增强篇)中讲述了针对LDS通知获取最新版本数据使用。在winter23的v56版本中,此方法还在正常使用,在 spring23的v57版本中,getRecordNotifyChange方法已被标记弃用,官方推荐notifyRecordUpdateAvailable方法,功能相同。

Salesforce LWC学习(二十九) getRecordNotifyChange(LDS拓展增强篇)-LMLPHP

notifyRecordUpdateAvailable方法和 getRecordNotifyChange传递形参一样,针对此方法,分成两步走。

1. 头部引入:import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';

2. 需要刷新的地方使用:notifyRecordUpdateAvailable(: Array<{recordId: string}>)

需要注意的是, recordId同样需要在user interface api支持的,否则不生效。

详情:https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_all_supported_objects.htm

接下来demo进行参考。

import { LightningElement, wire,api,track } from 'lwc';
import { getRecord,notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import saveAccount from '@salesforce/apex/RecordNotifyChangeController.saveAccount';
import getAccount from '@salesforce/apex/RecordNotifyChangeController.getAccount';
import PHONE_FIELD from '@salesforce/schema/Account.Phone';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class RecordNotifyChangeSample extends LightningElement {
    @api recordId;
    @track phone;
    @track industry;
    @track accountName;
    fields=[PHONE_FIELD,INDUSTRY_FIELD];
   accountRecord;

   @wire(getAccount,{recordId : '$recordId'})
   wiredAccount(value) {
       this.accountRecord = value;
       const { data, error } = value;
       if(data) {
           this.industry = data.Industry;
           this.phone = data.Phone;
           this.accountName = data.Name;
       }
   }


    handleChange(event) {
        if(event.target.name === 'phone') {
            this.phone = event.detail.value;
        } else if(event.target.name === 'industry') {
            this.industry = event.detail.value;
        }
    }

    async handleSave() {
        await saveAccount({ recordId: this.recordId, industry : this.industry, phone : this.phone})
        .then(result => {
            if(result === 'success') {
                refreshApex(this.accountRecord);
                notifyRecordUpdateAvailable([{recordId: this.recordId}]);
            } else {
                //TODO
            }
        })
        .catch(error => {
            //TODO
        });
    }

}
02-12 22:14