Tag Archives: PageMessage

Salesforce Display Button On Apex Page Message

Apex Class:

public class SampleController {
    
    public Account acc {get;set;}
    public Boolean isButtonVisible {get;set;}
    public string message {get;set;}
    
    public SampleController(){
        isButtonVisible = false;
        acc = new Account();
        message = '';
    }
    
    public pageReference showMessage(){
        isButtonVisible = true;
        message = 'Do you want to create the account';
        return null;
    }
    
    public pageReference saveAccount(){
        insert acc;
        isButtonVisible = false;
        acc = new Account();
        return null;
    }
    
    public pageReference cancel(){
        isButtonVisible = false;
        acc = new Account();
        return null;
    }
}

Visualforce Page:

<apex:page controller="SampleController" tabStyle="Account">
    <apex:form >
        <apex:pageMessages escape="false"/> 
        <apex:outputPanel rendered="{!isButtonVisible}">
            <apex:pageMessage severity="info" strength="3" summary="{!message}">
                <apex:commandButton value="Yes" action="{!saveAccount}"/>
                <apex:commandButton value="No" action="{!cancel}"/>
            </apex:pageMessage>
        </apex:outputPanel>
        
        <apex:pageblock title="Account">
            <apex:pageBlockSection title="Account Information" columns="2">
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.AccountNumber}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Confirm" action="{!showMessage}"> </apex:commandButton>
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

Line Break in Apex PageMessage

You can add \n for HTML NewLine character in Apex String, and you can replace each \n by <br/> in apex:pageMessage using JSENCODE function.

Apex Controller:

public class SFController{  
    public String msg {get;set;}
    public SFController(){  
        msg = 'Error Message 1' + '\n' + 'Error Message 2' + '\n' + 'Error Message 3';  
    }  
}

Visualforce Page:

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

Output: