1
export default class ShowToast extends LightningElement {
    @api recordId;
    account;
    lastModifiedDate;
    createddate;
    check=true;
    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    wiredRecord({ error, data }) {
        if (error) {
            console.log('error occured');

        } else if (data) {
            this.account = data;
            console.log('Log after isloading account>>>>>>', this.account);
            let modifiedDate=this.account.lastModifiedDate;
            //let createdate=this.account.createddate
            if(!this.lastModifiedDate) {
                this.lastModifiedDate = this.account.lastModifiedDate;
                console.log('Console lastmodified date---->',this.lastModifiedDate);
            }
            if (modifiedDate != this.lastModifiedDate || modifiedDate == this.lastModifiedDate ) {
                console.log('Console modified date---->',this.lastModifiedDate,modifiedDate);
                let rate = getFieldValue(this.account, RATING_FIELD) ?getFieldValue(this.account, RATING_FIELD) : ''
                let usrfield = getFieldValue(this.account, USER_FIELD) ?getFieldValue(this.account, USER_FIELD) : ''           
                console.log('Need user available field from user here>>'+ usrfield);

                if(rate =='Hot')
                {
                    console.log('rate debug---->',rate);
                    alert('YOU HAVE SELECTED ACCOUNT RATING AS HOT');
                }

                if(usrfield) //.User_Available__c =='True'
                {
                    console.log('Console user usrfield123>>>',usrfield);
                    alert(' User is  Not Selected ');                                    
                }

                this.showNotification();

            }

Every thing is working good I just dont want to show alert message when i refresh the account record detail page

3
  • You will need to store information in the database that allows you to identify when the toast was last shown, to see if it doesn't need to be shown again. Right now you rely on data stored temporarily in the UI and this will be lost each time you refresh the page. Rather than store in the database you could store per-account information in the browser's persistent store.
    – Phil W
    Aug 16 at 8:15
  • information is getting stored when i press save button
    – mike
    Aug 16 at 8:18
  • The information I mention is when the toast was last shown, and this needs to be saved at the time the toast was shown, not on save, if you don't want to show the toast more than once for a given account in a given case for showing the toast.
    – Phil W
    Aug 16 at 8:23

1 Answer 1

3

Everytime the page is refreshed, the component goes through its lifecycle flow, so the wire method runs and if those conditions are met it will show the alert.
In order to avoid this, you must set a flag the first time the alert is shown and use it in the if condition.
Since you want to persist this flag among several page refresh, you could leverage Session Storage: in the if block you could set a key composed by recordId and an unique value, i.e. "hot" or "user" (the if condition). Of course you're going to execute the if block only if that key doesn't exist.

if (modifiedDate != this.lastModifiedDate || modifiedDate == this.lastModifiedDate ) {
    console.log('Console modified date---->',this.lastModifiedDate,modifiedDate);
    let rate = getFieldValue(this.account, RATING_FIELD) ?getFieldValue(this.account, RATING_FIELD) : ''
    let usrfield = getFieldValue(this.account, USER_FIELD) ?getFieldValue(this.account, USER_FIELD) : ''           
    console.log('Need user available field from user here>>'+ usrfield);

    const alertHotKey = `${this.recordId}_hot`;
    const alertHotAlreadyShown = sessionStorage.getItem(alertHotKey);

    if(!alertHotAlreadyShown && rate == 'Hot')
    {
        console.log('rate debug---->',rate);
        sessionStorage.setItem(alertHotKey, 'true');
        alert('YOU HAVE SELECTED ACCOUNT RATING AS HOT');
    }

    const alertUserFieldKey = `${this.recordId}_user_field`;
    const alertUserFieldAlreadyShown = sessionStorage.getItem(alertUserFieldKey);

    if(!alertUserFieldAlreadyShown && usrfield) //.User_Available__c =='True'
    {
        console.log('Console user usrfield123>>>',usrfield);
        sessionStorage.setItem(alertUserFieldKey, 'true');
        alert(' User is  Not Selected ');                                    
    }

    this.showNotification();

}

By the way if (modifiedDate != this.lastModifiedDate || modifiedDate == this.lastModifiedDate) is just a complex way to write if(true) because those conditions are in OR, so no matters if modifiedDate is equals or not to this.lastModifiedDate that block will always be executed.

8
  • Thanks RubenDG. it worked. can you explain const alertUserFieldKey = ${this.recordId}_user_field;
    – mike
    Aug 16 at 9:34
  • @mike The first param of setItem() and the one of getItem() is the key (string) you want to set/get, since you must use the same key for both method, I declared them, the one for Hot and the one for User, only once (const alertUserFieldKey = `${this.recordId}_user_field`;) instead of writing twice the same template string. Using the recordId as prefix guarantees its uniqueness in the client session storage
    – RubenDG
    Aug 16 at 10:02
  • i have one more question to ask. How can we compare old and new values here. I have passed rate to alerthotKey but I am getting the id in console. I want to check old value of rate and current value of rate.
    – mike
    Aug 16 at 12:35
  • @mike If the LWC did the update (i.e. calling updateRecord or an apex method), you can store the previous values in an object, but if another process did the update you can get the old value only if you have the history enabled; in that case you can call an apex method that will query the object history.
    – RubenDG
    Aug 16 at 13:22
  • how the history will be enabled in this code, and what changes will be made in code. I am not getting it
    – mike
    Aug 17 at 7:27

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.