During my working on converting existing pages to be lightning experience style (as known as slds style). One of the challenges I have faced is to convert <apex:pageMessages/>
. Usually the controller side of code is something like below:
public class ConvertPageMessagesController{
public ConvertPageMessagesController(){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));
}
}
There are definitely walkaround on this. I can use a page_message and a serverity_level property and check the page status and the page message and use front-end code to handle the logic. But the code will inevitable be long and tedious. Is there a good way of handling this?
Best Answer
Here is what I do:
Class used by all my controllers:
VF Messages Abstract:
All controller for VF extend this class
Then in my vf pages I use the following:
Messages Toast
Helper Panel
Check Messages Code (Using jQuery)
My buttons, action functions, etc rerender the
post_processing
andmsg_block
Creating an error In the controller when I catch or want to display an error I simply:
This allows me to control the messages, when to display it and which theme to show.
Note: The
xmlns=
on thediv
containing the SVG is a MUST as if you try to rerender without it the page will simply stall. Important trick to remember for anytime you are re rendering a container containing an SLDS SVGIf you need a example all put together let me know.
End result is (Error):
Or (Confirm)
I also have implemented a way to pass messages between transactions so if you need to say DML on button click and then Callout oncomplete of button click, if there was an error in the first step, when the second step happens it does not clear the message like it normally would (not shown in code)