1

I have a scenario, wherein I display a Warning message using toast when the record is updated to meet certain criteria. I am trying to use a combination of @wire and imperative apex.

Below is the code snippet:

@api recordId;
    bShowToast = false;
    error;
    @wire (getRecord, {recordId : '$recordId' ,fields: [ 'Claim__c.StartDate__c']})
    getClaimRecord({error , data}){
        if(data){
            // eslint-disable-next-line no-console
            console.log('data : ' ,data);  
        }else if(error){
            this.error = error;
        }
        this.showToast();
    }
showToast() {
        verifyEndDate({sClaimId: this.recordId}) //imperative apex call
        .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: 'ACTION REQUIRED...',
                })
                );
            }
        })
        .catch(error => {
            // eslint-disable-next-line no-console
            console.log('Error Occured : ' ,error);
        });
    }

I send the value from apex method to display toast based on certain values on the record (majorly depend on changes to a single field).

Now, the problem is that it retains the value from very first apex call and the value doesn't change even if the back-end method is supposed to send other value. Eg: if on page/component load - the method returns true, it'll display same value even if I update the record to make the value as false. I tried using refreshApex() but that didn't work for me.

Any idea how this can be achieved.

When trying to use Aura component to force refresh.. I can't even find the aura component on custom component list. I have no experience with aura and directly started with LWC.. Below is the code for aura components:

<aura:component >
    <c:claimWarningMessage onrecordChange="{!c.refreshView}" ></c:claimWarningMessage>
</aura:component>

CONTROLLER:

({
    refreshView: function(component, event) {
    // refresh the view
    $A.get('e.force:refreshView').fire();
    }
})

I added below line in showToast() method...

this.dispatchEvent(new CustomEvent('recordChange'));

Any idea why I'm not able to see the component in custom component list?

  • You are updating that value on UI or in backend by some other process? – Pranay Jaiswal Nov 19 '19 at 20:22
  • @PranayJaiswal I'm updating it on record page itself where the component is placed. – Ashutosh Arora Nov 19 '19 at 20:36
  • verifyEndDate only contains recordId are you also sending the updated value of record? – Pranay Jaiswal Nov 19 '19 at 20:39
  • @PranayJaiswal I'm only sending the recordId and the apex method queries the record using the id. Ideally, apex method should be able to retrieve the updated record and send back the appropriate value but that's not happening at all. – Ashutosh Arora Nov 19 '19 at 20:42
  • So, there is another component, page that updates the value of record, but the your lwc component still shows the old value? – Pranay Jaiswal Nov 19 '19 at 20:44
0

You are using standard record SF save. The record Save will fire refreshView event. Unfortunately, lwc cant listen to refresh view event natively and react to it. You have to use an aura wrapper around it.

c:customWrapper.html

<aura:component>

<aura:handler event="force:refreshView" action="{! c.refreshLWC}" />
<c:customLWCComponent aura:id="myLWCCompnent"></c:customLWCComponent>
</aura:component>

Aura Controller:

refreshLWC: function(component, event, helper) {
        component.find("myLWCCompnent").refresh();
    },

Then in your lwc:

 @track backupObjClaim;
 getClaimRecord(result){
        this.backupObjClaim= result;

}


@api 
    refresh(){//this method is called from aura
        return refreshApex(this.backupObjClaim);
    }

src: https://salesforce.stackexchange.com/a/257396/19118

| improve this answer | |
  • I tried doing something similar but it doesn't work.. I'm not able to see the aura component in list of custom components.. I have updated the question.. please scroll down to bottom – Ashutosh Arora Nov 19 '19 at 22:04
  • You have to create one of your own. That will contain your lwc – Pranay Jaiswal Nov 19 '19 at 22:05
  • that's exactly what I did.. – Ashutosh Arora Nov 19 '19 at 22:07
  • Why are you firing $A.get('e.force:refreshView').fire();, you have to listen to this event. – Pranay Jaiswal Nov 19 '19 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.