I have added a custom button on the Opportunity page which invokes an apex code. The invoked apex code is redirecting to the same Opportunity page.

    PageReference pg = new PageReference('/'+ oppId +'?test=true'); 
    return pg;

I would like to add error message after the redirect to the Opportunity page.

Please advise how this can be achieved?

1 Answer 1


You cannot do both at the same time, either you want to redirect or you want to show an error.

I would suggest you:

  1. Create a variable to store the message you want to show
  2. On the VF page rerender the sections after the action that currently causes the redirect
  3. Render sections based on the message being present
  4. In the section that displays the message provide a button for the user to go back to the opportunity
  5. on click of the button the current page reference gets executed.

You could also add a timer on display of the message to have it redirect to the opportunity automatically.

Unfortunately, you do not provide enough information in your question to provide a more detailed response.

  • Thank you for your answer. Instead of creating new VF page is it possible to use javascript. It will look a lot more presentable if I create a dialog popup window to appear with message. The message will be displayed only for few seconds. If what I want to achieve is feasible I will strongly appreciate if you can include and example or some piece of code. Nov 30, 2016 at 21:09
  • You do not have to create a new VF page just a controller. But yes, you could do it via JS. You will have to be ALOT more specific for code sample to be given. Like exactly the steps in order of what you want to occur. And you should at least try something first
    – Eric
    Dec 1, 2016 at 0:53
  • I have added a custom button which is associated to a custom VF page which invokes an apex code. Currently the invoked apex code is redirecting to the same Opportunity page. Dec 1, 2016 at 21:06
  • 1. Instead of redirecting I'd like to pass a parameter to the custom VF page. 2. After the VF page get the parameter it will be necessary to display javascript pop-up window with message. 3. After the user press "OK" button in the javascript pop-up window, only then the VF page to redirect to same opportunity record. Dec 1, 2016 at 21:07
  • Could you please give a code example for all the steps because I'm not aware hot to pass a parameter from apex code to the current VF page and how to redirect from custom VF page to standard page? Dec 1, 2016 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .