[SalesForce] LWC: How to Use Wire Method and ShowToast correctly

So I've got a project that uses a screen flow to update opportunities with a related payment record. Once the record is linked to the opp, I have a lightning component that displays summary data of the newly linked record. When the opp goes from a state of no payment record –> a state where there is a new payment record, I want to show a toast message that says "Successfully linked payment"

Problem Currently, it is showing the success message every time the opp loads or changes. This is expected because the wire method reloads everything when the record it's connected to changes. However, is there a way to make sure that the toast message only shows in the following scenarios.

Essentially I want to determine of the related record has been added and if it is valid, show the toast message, but not do this every time someone visits the opp record.

Thanks.

import { LightningElement, api, track, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import {getRecord} from 'lightning/uiRecordApi';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';


//THESE have been imported with the '@salesforce/schema/OBJECTNAME.FIELDNAME syntax.
const FIELDS = [
    OPPORTUNITY_CUSTOMER_CONTACT,
    OPPORTUNITY_CUSTOMER_PAYMENT_METHOD,
    PAYMENT_METHOD_CONTACT,
    PAYMENT_METHOD_CVV_CHECK,
    PAYMENT_METHOD_BRAND,
    PAYMENT_METHOD_STATUS,
    PAYMENT_METHOD_ERROR_CODE,
    PAYMENT_METHOD_ERROR_MESSAGE,
    PAYMENT_METHOD_ERROR_PARAM,
    PAYMENT_METHOD_ERROR_TYPE
];

@api recordId; //opp Id
    @api objectApiName;
    @api paymentRecordId;
    wiredOppResult; //stores the wired result and allows the refreshApex() to work correctly
    @track record;
    @track error;
    @track objectInfo;


@wire(getRecord, {recordId: '$recordId', fields: FIELDS})
    getOppRecord(result){
        console.log('wiredMethodCalled');

        this.wiredOppResult = result;
        let data = result.data;
        let error = result.error;
        if(this.firstRun == false){
            this.firstRun = true;
        }

        if(data){

         this.checkPaymentStatus(this.paymentMethodStatus);
        }

//methods 

checkPaymentStatus(status){
        if(status == 'Valid'){
            //removing the this.showToast condition here makes the success message happen every time the record is loaded or changed. Which creates a notification fatigue. 
            if(this.showToast === true){
                this.MyShowToast('LINKED PAYMENT', 'Payment Method is valid and linked', 'Success');
            }

Best Answer

You can have the Flow show the toast message instead of your lightning component.

Related Topic