0

I have created a lightning web component which simply displays a warning message on opportunity record page if the stage is 'Prospecting'.

Now, from what I understand about wire method, if the record is updated the method is called again, pulling in the updated data.

So, if I update the stage to 'Needs Analysis', no warning message is displayed(because the code says it should only be shown when Prospecting). However, when I change it back to 'Prospecting', it still doesn't show the message and only works when I refresh the page.

Below is the code:

JS

import { LightningElement , api,  wire} from 'lwc';
import SHOW_TOAST from '@salesforce/apex/opportunityToast.verifyStage';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class WireMethodApex extends LightningElement {
    @api recordId;
    bShowToast = false;
    @wire (SHOW_TOAST,{recordId : '$recordId'})
    Flag({ data , error}){
        if(data) {
            this.bShowToast = data;
            if(this.bShowToast)
                this.showToast();
        }
        if(error) {
            console.log('Error Occured');
        }
    }
    showToast(){
        const evt = new ShowToastEvent({
            title: 'Need progress on Opportunity!!',
            message: 'Change the stage from Prospecting once progress made!',
            variant: 'warning',
            mode: 'dismissable'
        })
        this.dispatchEvent(evt);
    }
}

Apex Class

public class opportunityToast{
    @AuraEnabled(cacheable=true)
    public static boolean verifyStage(string recordId){
        boolean bShowToast = false;
        if(String.isNotBlank(recordId)){
            Opportunity oppt = [SELECT StageName 
                                FROM Opportunity
                                WHERE Id =: recordId];
            if(oppt.stageName.equalsIgnoreCase('Prospecting'))
                bShowToast = true;
        }
        return bShowToast;
    }
}

So, my question here is - How do I trigger/invoke the wire method whenever record is edited/saved.

2 Answers 2

0

The client record cache is only updated if:

  • A Lightning web component mutates the record.
  • The LDS cache entry expires and then a Lightning web component’s @wire triggers a read. The cache entry and the Lightning web component must be in the same browser and app (for example Lightning Experience) for the same user.

In your case, nothing is triggering a read because the change is happening on the backend and the cache has not expired yet. See https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_ui_api

If you need immediate notification of a change happening to an object, you might subscribe to change data capture using the empAPI.

0

In addition to the answer by @Nathan Fig, I am sharing the link explaining why the data is stale when you call the imperative apex from the wire service.

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_result_caching

The data is cached at the client's end i.e. browser and it takes time to refresh. To do that manually you need to use refresh apex or go with the Lightning Data Service.

Sharing a sample code with LDS -

import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import STAGE from '@salesforce/schema/Opportunity.StageName';

export default class OpportunityStageChange extends LightningElement {
  @wire(getRecord, { recordId: '$recordId', fields: [STAGE] })
  checkOpportunityStage({ error, data }) {
    if (data) {
      console.log('Data', data);
      console.log('OUTPUT : ', data.fields.StageName);
      if(data.fields.StageName.value === 'Needs Analysis'){
          this.showModal = open;
      }
    } else if (error) {
      console.error('Error:', error);
    }
  }
}

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_ui_api

Lightning Data Service manages data for you; changes to a record are
reflected in all the technologies built on it. Contrastingly, data
from Apex is not managed; you must refresh the data.

If Lightning Data Service detects a change to a record or any data
or metadata it supports, all components using a relevant @wire
adapter receive the new value.

You must log in to answer this question.

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