1

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.addMessage(new 
            ApexPages.Message(ApexPages.Severity.ERROR, 'Error.'));

     errorPage.setRedirect(true);
     errorPage.getParameters().put('cid',conID);

     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 May 8 at 6:44
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>
        </apex:pageBlock>
    </apex:form>
</apex:page> 
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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.