You need to sign in to do that
Don't have an account?
Ella
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);
}
})
<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);
}
})
find your solution from following way. 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
let me know if it helps you and mark it as best answer.
Thank you
All Answers
I don't understand what should I find on the link.
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!
find your solution from following way. 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
let me know if it helps you and mark it as best answer.
Thank you