You need to sign in to do that
Don't have an account?
Rebekah Lilly
How To Refresh LWC
Hello,
I have a very simple LWC that uses getRecord from 'lightning/uiRecordApi' to retrieve an Account record and two custom fields. It displays a banner message based on the values in the two fields. It does not update the fields, just retrieves the values. The field values, however, are being updated by a separate process that uses a future call for external processing. If I refresh the page, the banner changes or goes away, as it should. I would like it to refresh automatically once the fields are updated, without having to refresh the page manually. I have read about the getRecordNotifyChange, but I am unsure how to use it if the LWC is not making the changes. If I created an Apex class and used it, I know I could do a refreshApex, but again, this LWC is not making any changes, so it would need to do it after a delay to allow time for the external process to finish. Is there a better way to poll for record changes and refresh the LWC when these two values change?
import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import FIELD1 from '@salesforce/schema/Account.Field1__c';
import FIELD2 from '@salesforce/schema/Account.Field2__c';
const fields = [FIELD1, FIELD2];
export default class Status_Display extends LightningElement {
@api recordId;
status1;
status2;
alert = false;
invalid = false;
@wire( getRecord, { recordId: '$recordId', fields })
account({error, data}) {
if (error) {
this.alert = false;
this.invalid = false;
}
else if (data) {
this.status1 = data.fields.Field1__c.value;
if (this.status1 === 'Alert') {
this.alert = true;
}
this.status2 = data.fields.Field2__c.value;
if (this.status2 === 'Invalid') {
this.invalid = true;
}
}
}
}
I have a very simple LWC that uses getRecord from 'lightning/uiRecordApi' to retrieve an Account record and two custom fields. It displays a banner message based on the values in the two fields. It does not update the fields, just retrieves the values. The field values, however, are being updated by a separate process that uses a future call for external processing. If I refresh the page, the banner changes or goes away, as it should. I would like it to refresh automatically once the fields are updated, without having to refresh the page manually. I have read about the getRecordNotifyChange, but I am unsure how to use it if the LWC is not making the changes. If I created an Apex class and used it, I know I could do a refreshApex, but again, this LWC is not making any changes, so it would need to do it after a delay to allow time for the external process to finish. Is there a better way to poll for record changes and refresh the LWC when these two values change?
import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import FIELD1 from '@salesforce/schema/Account.Field1__c';
import FIELD2 from '@salesforce/schema/Account.Field2__c';
const fields = [FIELD1, FIELD2];
export default class Status_Display extends LightningElement {
@api recordId;
status1;
status2;
alert = false;
invalid = false;
@wire( getRecord, { recordId: '$recordId', fields })
account({error, data}) {
if (error) {
this.alert = false;
this.invalid = false;
}
else if (data) {
this.status1 = data.fields.Field1__c.value;
if (this.status1 === 'Alert') {
this.alert = true;
}
this.status2 = data.fields.Field2__c.value;
if (this.status2 === 'Invalid') {
this.invalid = true;
}
}
}
}
import { LightningElement, api } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { getRecordNotifyChange } from "lightning/uiRecordApi";
import updateAsCustomer from "@salesforce/apex/UpdateTypeActionController.updateAsCustomer";
export default class UpdateTypeAction extends LightningElement {
@api
recordId;
@api
objectApiName;
@api
async invoke() {
await updateAsCustomer({
accountId: this.recordId
});
this.dispatchEvent(
new ShowToastEvent({
title: "Success",
message: 'Account type updated to "Customer"',
variant: "success"
})
);
getRecordNotifyChange([{ recordId: this.recordId }]);
}
}