2

Is it possible to pass a page message to a newly created PageReference, so that this page will display it?

public PageReference doAction() {
   PageReference resultPage = new PageReference('/'+id);
   resultPage.addMessage(new ApexPages.Message(...));

   return resultPage;
}

or

public PageReference doAction() {
   PageReference resultPage = Page.customPage;
   resultPage.addMessage(new ApexPages.Message(...));

   return resultPage;
}
3

You cannot add a message like this as it is stored in the page state, which gets lost when you redirect. Scenario 1 is out of the question as your PageReference points to a non-VF page. However, for scenario 2, why not just pass a URL parameter that the controller can read when the page loads, and create the appropriate message at that time.

| improve this answer | |
1

The following is only relevant to your second example, its not possible to share state between pages based on different controllers (as per ScottW's answer).

Sharing State between Pages with the Same Controller. I've just tried this, which I am sure I've seen work before and is documented, but I'm getting a corrupted page back from Salesforce as result of the redirect. I've logged it as an answer just to illustrate what I've tried, as I thought it might have worked and still feel it should, perhaps a Salesforce bug?

<!-- messagepage1 -->
<apex:page controller="MessagePageController" action="{!doAction}">
</apex:page>

<!-- messagepage2 -->
<apex:page controller="MessagePageController">
    <apex:pageMessages />
</apex:page>

public with sharing class MessagePageController {
    public PageReference doAction()
    {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Test message'));
        return Page.messagepage2;
    }
}

enter image description here

As per the docs...

If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

So I also tried...

public PageReference doAction()
{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Test message'));
    PageReference page2 = Page.messagepage2;
    page2.setRedirect(false);
    return page2;
}

Without success either, very strange. So it may look like the answer is to pass the information as a parameter as per this question Show Apex message at another page

| improve this answer | |
  • 1
    I've successfully passed messages through pages like this before, but as the documentation says, the controller and extensions must match, or it will be lost. In theory, the code in this answer should work. You may need to check the debug logs to see what's going on. – sfdcfox Nov 14 '13 at 15:48
  • 1
    Yeah same here, really confused at this, nothing in the debug logs that jumps out, gets to constructing the page ref and exits. – Andrew Fawcett Nov 14 '13 at 15:52
  • 1
    Just checked my API versions and updated to 29, no dice! :-( – Andrew Fawcett Nov 14 '13 at 15:54
  • 1
    It doesn't work with redirects (page action) or reRender attributes, as far as I can tell. This page setup works: – sfdcfox Nov 14 '13 at 16:24
  • 1
    public with sharing class asktellmessagecontroller { public pagereference tellmessage() { apexpages.addmessage(new apexpages.message(apexpages.severity.confirm, 'Hello There!')); return page.tellmessage; } } <!-- ask message --> <apex:page controller="asktellmessagecontroller"> <apex:form id="form"> <apex:commandButton value="Tell Me!" action="{!tellmessage}"/> </apex:form> </apex:page> <!-- tellmessage --> <apex:page controller="asktellmessagecontroller"> <apex:pageMessages/> </apex:page> – sfdcfox Nov 14 '13 at 16:24

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