0

I have a LWC component which displays a text status of contact and is updated whenever a formula field is changed.

I need to reload the page to view the changes. Currently i am using the wire function to fetch the data is there any way through which if the formula field is updated then the banner is automatically updated without page load. This is the current code used to fetch the status from contact object.

import { LightningElement,api,wire } from 'lwc';
import getWorkPermitEligibilityStatus from '@salesforce/apex/WorkEligibilityBannerClass.checkEligilityStatus';
import EligibleWorkPermitStatusLabel from '@salesforce/label/c.EligibleWorkPermitStatus';
import NonEligibleWorkPermitStatusLabel from '@salesforce/label/c.NonEligibleWorkPermitStatus';

export default class WorkEligibilityBanner extends LightningElement {
@api validWorkPermitMessage = false
@api workPermitExpiredNotVerifiedMessage = false
@api recordId;
eligibilityMark = '';
error='';
 

label = {
    EligibleWorkPermitStatusLabel,
    NonEligibleWorkPermitStatusLabel
 };
 

@wire(getWorkPermitEligibilityStatus, {conId: '$recordId'})
WireContactRecords({error, data}){
        if(data){
            console.log(data)
            if(data === 'validWorkPermitNeeded'){
                this.validWorkPermitMessage = true;
            }
            else if(data === 'workPermitExpiredNotVerified'){
                this.workPermitExpiredNotVerifiedMessage = true;
            }
            this.error = undefined;
        }else{
            this.error = error;
            this.eligibilityMark = undefined;
            
        }
    }
}

2 Answers 2

1

Formula fields don't "change" in the database, as such, but rather are calculated at the time of query. That's why there's no Change Data Capture for formula fields.

You would need to consider adding an Apex trigger or similar that compares the formula values (e.g. between Trigger.old and Trigger.new for the same record) and generates some form of event that your LWC listens for using something like platform events and EMP.

The empApi documentation provides examples of how to do this.

Note that, since this is under the hood CometD-based, the notifications to your UI will necessarily consume available daily limits.

You should also note that a wire uses a client-side cache as well, so beware that even with the EMP you would need to cause the client-side cache to be invalidated. An option is to use refreshApex, though it is actually better to switch to uncached imperative Apex if you are always wanting the up-to-date value when the Apex is explicitly invoked. Check the documentation for more.

Adopting imperative, you could instead use a timer-delayed re-invocation of the Apex method to refresh the component every so many (e.g. 10) seconds just in case the status changed (using setTimeout). This avoids any limits use, at the cost of repeated calls from the client (the browser) to the server. Fortunately Salesforce doesn't count UI/server interactions against API limits.

1
  • Thanks for the detailed response.i would check this out
    – Dodo dodo
    Feb 6, 2022 at 10:34
0

You can use:

refresApex(this.WireContactRecords);

add to the top:

import {refreshApex} from '/@sales/apex';

That refresh the values under WireContactRecords.

You must log in to answer this question.

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