4

I want to display the message at another page when user click a button. Such as the user clicks button at Page A, then my action method will redirect user to Page B and show the message which is defined in the Page A controller.

Page A and Page B not use the same controller.

String pageHeaderReferer = ApexPages.currentPage().getHeaders().get('Referer'); 
if((pageHeaderReferer != null && pageHeaderReferer.containsIgnoreCase('Page1')) || isLtIE9()) 
{    
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, message)); 
} 

Be careful, using the above code in IE8 that there is no 'Referer' value.

7

The easiest way would be to pass the message as a parameter when redirecting the user, then have the second controller add that parameters content to the page.

Controller 1

public PageReference GoToPage2()
{
  ApexPages.PageReference pr = new ApexPages.PageReference(Page.Page2);
  pr.SetRedirect(true);
  pr.GetParameters().Put('message', 'Hello, World!');
  return pr;
}

Page 1

<apex:commandButton action="{!GoToPage2}" value="Page 2!"/>

Controller 2

public void Init()
{
  String message = '' + ApexPages.CurrentPage().GetParameters().Get('message');
  String pageHeaderReferer = ApexPages.currentPage().getHeaders().get('Referer'); 

  // Use the referrer parameter to only show the message when coming from Page 1
  if(pageHeaderReferer != null && pageHeaderReferer.containsIgnoreCase('Page1') && message != 'null')
  {
    ApexPages.CurrentPage().AddMessage(new ApexPages.Message(ApexPages.Severity.Info, message));
  }
}

Page 2

<apex:page controller="Page2Controller" action="{!Init}">
  <apex:pageMessages/>

The exact message syntax might be a little off, will double check it. It's one of those things I can remember when I'm coding but not when I'm just writing in the browser!

5
  • Thanks, but I think this will add a new parameter in the URL, if the user refresh the Page 2, the message will display again. I just want the message to display only one time. – Jair Jun 30 '13 at 4:15
  • You can always clear the parameter after reading it. An alternative mechanism would be writing it to a temporary object or custom setting (the latter might be better). – Matt Lacey Jul 1 '13 at 0:11
  • String pageHeaderReferer = ApexPages.currentPage().getHeaders().get('Referer'); if(pageHeaderReferer != null && pageHeaderReferer.containsIgnoreCase('Page1')) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, message)); } – Jair Jul 2 '13 at 6:22
  • @LaceySnr Is it possible to make a conditional display? I have a situation where 'Submit' and 'Cancel' buttons on page 1 are going to redirect to the Page 2 but I want to display different messages according to the button's action. or do I have post a separate question for this? – Seesh Nov 18 '14 at 0:13
  • @Seesh A new quesiton is best, you'll get rep that way! – Matt Lacey Nov 18 '14 at 2:08

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.