2

I have some fields from a record i display via lightning-record-view-form.

I have an Apex action invoked imperatively e.g. myMethod().then().catch(). That action changes the field on my object.

When the method returns successfully, i want the data in lightning-record-view-form to update. I tried setting the recordId to 'banana' and then setting it back to recordId, but that didn't work.

import STATUS_FIELD from "@salesforce/schema/My_Object__c.Status__c";

export default class Whatever extends LightningElement {
    @api recordId;
    @api objectApiName;
@track status = STATUS_FIELD.fieldApiName;
doStuff() {
  doApex().then(r => { // update? }).catch(e => console.log(e));
}
<template>
   <div if:true={recordReady}>
                <div>
                    <lightning-record-view-form record-id={recordId} object-api-name={objectApiName}>
                        <span>Status</span>
    <!-- We don't specify the name directly coz i am in a managed package with a namespace -->
                        <lightning-output-field field-name={status} variant="label-hidden">
                        </lightning-output-field>
                    </lightning-record-view-form>
                </div>

<lightning-button label="Do Stuff" onclick={doStuff} name="do"></lightning-button>

 <template>

3 Answers 3

5

I tried setting the recordId to 'banana' and then setting it back to recordId, but that didn't work

It is loading the record from cache and so the values will not be refreshed. If you wait for 30 seconds and do this, then the values will get refreshed from server.

As of today, we do not have a direct way of refreshing the record values in LWC forms when we know that the record is changed in database. For comparison in Aura, we have something like reloadRecord in LDS.

Workaround:

You can use refreshApex() on getRecord to manually refresh the cache immediately. You can refer to - How to refresh wired service getRecord manually?. Since you are using record-view-form, it will be using the same cache as that of getRecord and so it will refresh the form automatically. Also, you can just use 1 field Id to refresh the form. Below is the code which will work:

@wire(getRecord, { recordId: '$recordId', fields: ['Account.Id'] })
account;

doStuff() {
    doApex().then(r => refreshApex(this.account)).catch(e => console.log(e));
}
1
  • Thanks for the workaround!
    – Jan Julian
    Commented Nov 6, 2019 at 21:28
2

You can use getRecordNotifyChange

Fetch record updates for the specified record IDs and refresh the Lightning Data Service cache, providing your wires with the latest record data

import { getRecordNotifyChange } from 'lightning/uiRecordApi';

doStuff() {
 doApex().then(r => { 
  getRecordNotifyChange([{recordId: this.recordId}]);
 })
}
2
  • I tried this and it does not give me the effect i want, does not cause all @wire properties to refresh. Updating Id via updateRecord works a treat for that.
    – zaitsman
    Commented Mar 1, 2021 at 5:16
  • This works nicely with lighting-record-*-forms (especially to update stale roll-ups values) Commented Dec 17, 2021 at 20:20
0

Initially let recordReady variable be true on your button onclick set this to false bring in a spinner to get the loading effect once the response has come set this to true again and sore the response in a non trackable attribute and pass the value to the desired field on the onLoad() method of record edit form which a better choice for the scenario . Tried it on my end I could observe pre-population.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .