Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

up vote 6 down vote accepted

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!

share|improve this answer
 
+1 .Was drafting same and then just saw this :) –  Mohith Kumar Jun 30 at 3:47
 
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 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). –  LaceySnr Jul 1 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 at 6:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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