How we can pass some boolean value from apex controller to visualforce page as hidden parameter and access that value in that visualforce page?

For eg. In apex if we set flag = true after successful transaction, I need to set this flag value in hidden parameter of page reference.

In Visualforce page- I will check for flag value and display some message.

I have a contact id in URL and i am calling an apex method on click of command button which redirects to another page if success otherwise throw an exception which i am showing using apex.addMessage() and it should redirect to same page with contact id in URL.

I tried with below code-

 try {
           //save method
 } catch(Exception e){

            ApexPages.Message(ApexPages.Severity.ERROR, 'Error.'));


     return errorPage;

This is redirecting to the errorPage with Contact id in URL but not showing error message.

  • 2
    Please take a look at How to Ask and update your question to show what you've tried and where you are stuck. SFSE doesn't provide code towards requirements but can help with your specific issues.
    – Phil W
    Commented May 8, 2019 at 6:44

1 Answer 1


Once you redirect to another page you don't have apex:pageMessages any more.

<apex:page controller="Controller">
    <apex:form id="apexForm">
        <apex:pageMessages />
        <apex:commandButton action="{!testAction}" value="Test" reRender="apexForm"/>

        <apex:pageBlock rendered="{!flag}">
            <h1> Rendered only if flag is true</h1>
public with sharing class Controller {

    public Boolean flag {get;set;}

    public Controller() {
        flag = false;

    public PageReference testAction() {
        flag = true;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error'));
        return null;

You must log in to answer this question.

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