Wednesday, January 22, 2014

Break Line in a apex:pageMessage

Recently on a Salesforce Community, an User want to display error message on Visualforce page  using <apex:pageMessage> but this requirement is not easy like we are thinking he want message will be breaking into few lines.

I have searched on google, but i didn't find any result. So,I thought of finding the solution

and got it after struggling with many ideas.

Today, I am providing you that solution, it may be helpful for you.

Controller :

public class LineBreakController
{
    public String str{get;set;}
    public LineBreakController()
    {
       str = 'This is Line 1 \n This is Line 2';           //Your Error Message.
    }
}



VisualForce Page :

<apex:page controller="LineBreakController">
     <apex:pageMessage summary="{!SUBSTITUTE(JSENCODE(str ), '\\n', '<br/>')}" severity="warning" strength="3" escape="false" />
</apex:page>

After Use this code you will get the result something like -