1

I came across an easy way to Show Apex message at another page (code is given below). I have a situation where 'Save' and 'Cancel' buttons on page 1 are going to redirect to the Page 2 and display messages according to the button's action.' The best way to include a condition for displaying the message? Thanks.

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/>
2
  • Not sure exactly what your question is... are you wanting to limit the message based on the originating page? Are you wanting it to say x if they click Save or y if they click Cancel? would both actions take them to page 2? – BradByte Nov 18 '14 at 20:58
  • @BradBumbalough The buttons are in Page 1. Both the actions take them to page 2. Yes, If they click SAVE then display X in Page 2, if they click CANCEL display Y in Page 2. – Seesh Nov 18 '14 at 21:02
1

Here's a simple way I would do it.... let me know if you are needing something different. Thanks!

Page:

<apex:commandButton value="Save" action="{!GoToPage2}">
   <apex:param name="action" assignTo="{!action}" value="Save"/>
</apex:commandButton>
<apex:commandButton value="Cancel" action="{!GoToPage2}">
   <apex:param name="action" assignTo="{!action}" value="Cancel"/>
</apex:commandButton>

Controller:

public String action {get; set;}
public PageReference GoToPage2()
{
  ApexPages.PageReference pr = new ApexPages.PageReference(Page.Page2);
  pr.SetRedirect(true);
  pr.GetParameters().Put('message', 'Hello, World!');
  //pass a new param to the page of action
  pr.GetParameters().Put('action', action);
  return pr;
}
2
  • You are right. I've followed and created in the same way and is working fine. Thank you @BradBumbalough. – Seesh Nov 18 '14 at 22:32
  • Great! glad it worked. – BradByte Nov 19 '14 at 12:30

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.