0

I have written save method in helper. I am able to save record but toast message is not displaying.

Below is the save method.

 saveAction.setCallback(this,function(response){
                var accId = response.getReturnValue();
                var toastEvent = $A.get("e.force:showToast");
                        toastEvent.setParams({
                            "title": "Success!",
                            "message": "The record has been updated successfully."
                        });

                if (response.getState() == "SUCCESS") {                         

                    if(accId.startsWith('001')){                          

                        window.location.href = "/"+accId;
                    }
                    else{
                        component.set("v.accError",accId);
                        component.set("v.isAccError",true);
                    }
                }
            });
             toastEvent.fire();
            $A.enqueueAction(saveAction);
0

2 Answers 2

4

Couple of things:

  • You will probably want to change the messaging in the toast based on the repsonse from the server.
  • Dont use window.location.href for navigating in lightning, use the framework events provided such as navigateToSobject or navigateToURL .
  • Please include some debugging details in your post, 'Not working' is not very helpful for others to provide feedback.
4
  • +1, although toastEvent is fine just where it is. It's in the right scope.
    – sfdcfox
    Jan 25, 2019 at 16:50
  • I would have thought that since the op is using var, it could be easily be put outside that block.
    – glls
    Jan 25, 2019 at 16:58
  • It can be moved outside, because JavaScript will create the appropriate closure for it, but it's not necessary or even necessarily advisable. One should get an event just immediately before they use it, and not try to reuse an event. Putting it in the correct scope removes the temptation to do something that the framework will error out on.
    – sfdcfox
    Jan 25, 2019 at 17:06
  • =) thanks for the additional input - ill remove that bulletpoint from my answer
    – glls
    Jan 25, 2019 at 17:35
0

Try this code, I have made some minute modifications. Hope it helps you.

({  
    saveAction.setCallback(this,function(response)){   
    var accId = response.getReturnValue();  
    var toastEvent = $A.get("e.force:showToast");  
    toastEvent.setParams({
    "title": "Success!",
    "message": "The record has been updated successfully."
});

if (response.getState() == "SUCCESS") {                         

    if(oppId.startsWith('001')){                          

        window.location.href = "/"+accId;
    }
    else{
        component.set("v.opportunityError",accId);
        component.set("v.isOpportunityError",true);
    }
}  
toastEvent.fire();  
}                               
//toastEvent.fire();

$A.enqueueAction(saveAction);

});
4
  • I tried it, record is saved but toast message is not displaying. Do I need to update anything in css or lightning component button ?
    – SFDC
    Jan 25, 2019 at 11:15
  • Also, share your component for better responses. Jan 25, 2019 at 11:23
  • 3
    Naveen, in addition to posting code, could you provide some explanation so that the poster and the community can understand what you have changed and why? That makes this post valuable to everyone.
    – David Reed
    Jan 25, 2019 at 12:51
  • I have commented on what I have changed from your code. Jan 29, 2019 at 6:16

You must log in to answer this question.

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