[SalesForce] LWC imperative apex not fetching updated record value

I'm working on a requirement where I display a toast on the standard record page based on certain record value.
So, I have embedded a custom lightning web component wrapped in an aura component on the standard record page.

Below is the code for LWC:

import { LightningElement, api, wire ,track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import verifyEndDate from '@salesforce/apex/ClaimWarning.verifyEndDate';
import { refreshApex } from '@salesforce/apex';
export default class claimWarningMessage extends LightningElement {
    @api recordId;
    bShowToast = false;
    @track backupObjClaim;
    @wire (getRecord, {recordId : '$recordId' ,fields: [ 'Claim__c.End_Date__c']})
    getClaimRecord(result){
        this.backupObjClaim = result;
        this.dispatchEvent(new CustomEvent('recordChange'));
        //this.objClaim = result;
        if(result.data){
            // eslint-disable-next-line no-console
            console.log('data : ' ,result.data);
            // eslint-disable-next-line no-console
            console.log('End Date : ' ,result.data.fields.End_Date__c.value);
            this.showToast();     
        }
    }

    showToast() {
         verifyEndDate({sClaimId: this.recordId})
        .then(result =>{
            // eslint-disable-next-line no-console
            console.log('result : ' ,result);
            this.bShowToast = result;
            // eslint-disable-next-line no-console
            console.log('bShowToast : ' ,this.bShowToast);
            if(this.bShowToast){
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Get Help',
                        message: 'My Error Message',
                })
                );
            }
        })
        .catch(error => {
            // eslint-disable-next-line no-console
            console.log('Error Occured : ' ,error);
        });


    }
    @api 
    refresh(){
        return refreshApex(this.backupObjClaim);
    }
}

Now, what I'm doing here is that I'm calling the showToast() method from wired method which does a back-end call using verifyEndDate by passing the record id, which then manipulates the record data and returns true/false based on End_Date__c field value.
Say initially the value of End Date is 11/30 and the back-end method return true -> which makes the toast appear on the standard record page.

However, if I update the record and change the value of End Date to 11/15 and save the record(this would lead to wire method invocation which calls the showToast() method which in turn calls apex imperatively), the back-end method call should now return false based on the updated value(false), but to the contrary, the console still prints the same value(true) and the toast still appears.

And if the initial value for End_Date is 11/15(returning false from back-end) and I update the value to 11/30(which should make the value true) I still get false in console..

Is it because the back-end method is not getting called and I'm able to see the result from cache.

As per suggestions, I had embedded the lwc component in aura as follows, but doesn't seem to resolve the issue as well.

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction">
    <aura:handler event="force:refreshView" action="{! c.refreshView}" />
    <c:claimWarningMessage aura:id="myLWCCompnent" recordId="{!v.recordId}" onrecordChange="{!c.refreshView}"></c:claimWarningMessage>
</aura:component> 

Controller:

({
    refreshView: function(component, event) {
        // refresh the view
        component.find("myLWCCompnent").refresh();
    },
})

Can someone help me fix this issue please?

Best Answer

Remove the (cacheable=true) from the Apex for your 'verifyEndate' method. This way the information you want will always be fresh when the component is loaded.

Related Topic