4

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?

4
  • Don’t just go by what console statements show. It could be the case that Apex method is not invoked the second time. Try adding system.debug in Apex method and see if it prints.
    – javanoob
    Nov 20, 2019 at 15:07
  • I have actually tried that already and the debug statements don't print at all for the second time.. But ideally, the backend method should be called because wire method would invoke everytime record changes leading to imperative apex call. Nov 20, 2019 at 16:06
  • Just curious, the verifyEndDate apex method updates the record or not? Your description says manipulates the record, so I didn't think you had this method annotated with cacheable=true
    – javanoob
    Nov 20, 2019 at 21:14
  • 1
    @javanoob the description says that the component resides on standard record page where data is saved and veifyEndDate returns true/false based on the value's of that record. Nov 21, 2019 at 12:53

1 Answer 1

3

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.

0

You must log in to answer this question.

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