function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EllaElla 

Toast message is displayed after reloading the page

I have an apex class that displays a toast message . I don't know why but my toast message is displayed only when I refresh the page , not when the record is open. How can I achieve this?

<aura:component  Controller="GetAccount" implements="lightning:availableForFlowScreens,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="record" type="Map" default="{}" />
    <aura:attribute name="disabled" type="Boolean" default="true" />
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <force:recordData targetFields="{!v.record}"
        recordId="{!v.recordId}"
        recordUpdated="{!c.recordLoaded}"
        fields="Has_Code_Red_Cases__c,Has_Critical_Situation_Request_Cases__c"/>
    
</aura:component>

Controller:

({
    doInit : function(component,event,helper){
        var action = component.get("c.getAccountList");
        var currentId = component.get("v.recordId");
        action.setParams({ "currentId":currentId });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var allValues = response.getReturnValue();
                if(allValues[0].Has_Code_Red_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has An SRC Code Red Case!',
                        message: record.Has_Code_Red_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }
             if(allValues[0].Has_Critical_Situation_Request_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has A Critical Situation Request Case!',
                        message: record.Has_Critical_Situation_Request_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }   
                
            }
        });
        $A.enqueueAction(action);
    },
    
    recordLoaded: function(component, event, helper) {
        component.set("v.disabled", false);
    }
 })

 
Best Answer chosen by Ella
ravi soniravi soni
hi Ella,
find your solution from following way.
<aura:component  Controller="fetchAccount" implements="lightning:availableForFlowScreens,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="disabled" type="Boolean" default="true" />
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <force:recordData targetFields="{!v.record}"
        recordId="{!v.recordId}"
        recordUpdated="{!c.recordLoaded}"
        fields="Has_Code_Red_Cases__c,Has_Critical_Situation_Request_Cases__c"/>
    
</aura:component>
 
({
    doInit : function(component,event,helper){
        var action = component.get("c.getAccountList");
        var currentId = component.get("v.recordId");
        action.setParams({ "currentId":currentId });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var allValues = response.getReturnValue();
                if(allValues[0].Has_Code_Red_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has An SRC Code Red Case!',
                        message: record.Has_Code_Red_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }
             if(allValues[0].Has_Critical_Situation_Request_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has A Critical Situation Request Case!',
                        message: record.Has_Critical_Situation_Request_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }   
                
            }
        });
        $A.enqueueAction(action);
    },
    
    recordLoaded: function(component, event, helper) {
        component.set("v.disabled", false);
    }
 })
I did some changes inyour code and now it's working fine.

In case if you find any error than you can keep an eye on my code once and take referance.

here is my code
 
<aura:component  Controller="fetchAccount" implements="lightning:availableForFlowScreens,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="disabled" type="Boolean" default="true" />
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <force:recordData targetFields="{!v.record}"
        recordId="{!v.recordId}"
        recordUpdated="{!c.recordLoaded}"
        fields="Name,Description,Phone,Industry,Type"/>
    
</aura:component>
 
({
    doInit : function(component,event,helper){
        var action = component.get("c.getAccRecords");
        var currentId = component.get("v.recordId");
        action.setParams({ "currentId":currentId });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var allValues = response.getReturnValue();
                if(allValues[0].Type == 'Prospect'){
                    
                    var record = component.get("v.record");
                    alert('record : ' + JSON.stringify(record)); 
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has An SRC Code Red Case!',
                        message: record.Type,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }
           /*  if(allValues[0].Has_Critical_Situation_Request_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has A Critical Situation Request Case!',
                        message: record.Has_Critical_Situation_Request_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }  */ 
                
            }
        });
        $A.enqueueAction(action);
    },
    
    recordLoaded: function(component, event, helper) {
        component.set("v.disabled", false);
    }
 })
 
public with sharing class fetchAccount {
   
    @AuraEnabled
    public static List<Account> getAccRecords(string currentId){
        List<Account> lstAccount = [Select Id,Name,Type FROM Account 
                                    WHERE Id =:currentId];
        system.debug('lstAccount---->'+lstAccount); 
        return lstAccount;
    }
}



let me know if it helps you and mark it as best answer.
Thank you

All Answers

Ashutosh kureelAshutosh kureel
latest sarkari yojana check out (https://www.bestsarkariyojana.com/)
EllaElla
Hello Ashutosh, 
I don't understand what should I find on the link.
 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

You are showing message on doInit method and as you know that doInit is a constructor when the class load doInit method is executed.

So as you refresh page doInit executed and at the same time your toast , message is fire.

So think about it,

Follow this link I hope it will help you.

https://salesforce.stackexchange.com/questions/312199/how-to-display-toast-message-on-record-opening

If you would understood the problem of your code .Please mark it as the Best Answer

Thank You!

 

 

Godrej AnandaGodrej Ananda
Godrej Ananda (https://www.godrej-ananda.co/) is a class apartment coming in Bagalur. You get a wonderful specifications and price in this project.
ravi soniravi soni
hi Ella,
find your solution from following way.
<aura:component  Controller="fetchAccount" implements="lightning:availableForFlowScreens,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="disabled" type="Boolean" default="true" />
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <force:recordData targetFields="{!v.record}"
        recordId="{!v.recordId}"
        recordUpdated="{!c.recordLoaded}"
        fields="Has_Code_Red_Cases__c,Has_Critical_Situation_Request_Cases__c"/>
    
</aura:component>
 
({
    doInit : function(component,event,helper){
        var action = component.get("c.getAccountList");
        var currentId = component.get("v.recordId");
        action.setParams({ "currentId":currentId });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var allValues = response.getReturnValue();
                if(allValues[0].Has_Code_Red_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has An SRC Code Red Case!',
                        message: record.Has_Code_Red_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }
             if(allValues[0].Has_Critical_Situation_Request_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has A Critical Situation Request Case!',
                        message: record.Has_Critical_Situation_Request_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }   
                
            }
        });
        $A.enqueueAction(action);
    },
    
    recordLoaded: function(component, event, helper) {
        component.set("v.disabled", false);
    }
 })
I did some changes inyour code and now it's working fine.

In case if you find any error than you can keep an eye on my code once and take referance.

here is my code
 
<aura:component  Controller="fetchAccount" implements="lightning:availableForFlowScreens,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="disabled" type="Boolean" default="true" />
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <force:recordData targetFields="{!v.record}"
        recordId="{!v.recordId}"
        recordUpdated="{!c.recordLoaded}"
        fields="Name,Description,Phone,Industry,Type"/>
    
</aura:component>
 
({
    doInit : function(component,event,helper){
        var action = component.get("c.getAccRecords");
        var currentId = component.get("v.recordId");
        action.setParams({ "currentId":currentId });
        
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var allValues = response.getReturnValue();
                if(allValues[0].Type == 'Prospect'){
                    
                    var record = component.get("v.record");
                    alert('record : ' + JSON.stringify(record)); 
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has An SRC Code Red Case!',
                        message: record.Type,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }
           /*  if(allValues[0].Has_Critical_Situation_Request_Cases__c == true){
                    
                    var record = component.get("v.record");
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'This Account Has A Critical Situation Request Case!',
                        message: record.Has_Critical_Situation_Request_Cases__c,
                        duration:' 5000',
                        key: 'info_alt',
                        type: 'warning',
                        mode: 'sticky'
                    });
                    toastEvent.fire();   
          
                }  */ 
                
            }
        });
        $A.enqueueAction(action);
    },
    
    recordLoaded: function(component, event, helper) {
        component.set("v.disabled", false);
    }
 })
 
public with sharing class fetchAccount {
   
    @AuraEnabled
    public static List<Account> getAccRecords(string currentId){
        List<Account> lstAccount = [Select Id,Name,Type FROM Account 
                                    WHERE Id =:currentId];
        system.debug('lstAccount---->'+lstAccount); 
        return lstAccount;
    }
}



let me know if it helps you and mark it as best answer.
Thank you
This was selected as the best answer
Godrej Ananda 12Godrej Ananda 12
Life at Godrej Ananda (https://www.godrej-ananda.net.in) is a world of new joys for you and your loved ones.