function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JasonGablerJasonGabler 

Cannot get apex:page's action="{!function}" to redirect to another page

This is for a Visualforce page on a Force.com Site.  The purpose here is to get a function to run upon page load that will redirect if an certain value is not present in the controller.  I've whittled all of that logic out for the purpose of testing the "action" functionality

 

The documentation for the <apex:page>  "action" attribute states:

 

 

  • The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action is not specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

 

 

 

To me this means:  you can execute a function when the page is requested, and in that function you can get your end user to land on an entirely different page.

 

Here's my code:

 

 

<apex:page cache="false" sidebar="false" showHeader="false" controller="RFQ_Wizard_Controller" action="{!checkId}" > 

 

 

and, I've tried these two action methods

 

 

  public String checkId() {
     return Page.RFQ_Not_Found.getUrl;
 }

... o r...

  public PageReference checkId() {
     return Page.RFQ_Not_Found;
 }

 

I've done some debugging, and I have verified that they do, in fact, execute.  Is there some way within Apex that I can force the controller to redirect to another page?   It seems that the Visualforce's apex:page "action" doesn't actually do anything with this output of the action method.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JasonGablerJasonGabler

Geez... so I fiddled around a bit more.  Its too easy.

 

 

  public PageReference checkId() {
     PageReference not_found =  Page.RFQ_Not_Found;
     not_found.setRedirect(true);
    return not_found;
 }

 

 

I truly hope someone benefits from my wasted time...

 

jason

All Answers

JasonGablerJasonGabler

Geez... so I fiddled around a bit more.  Its too easy.

 

 

  public PageReference checkId() {
     PageReference not_found =  Page.RFQ_Not_Found;
     not_found.setRedirect(true);
    return not_found;
 }

 

 

I truly hope someone benefits from my wasted time...

 

jason

This was selected as the best answer
bob_buzzardbob_buzzard

Out of interest, do the pages that are you trying to redirect to use the same controller?  If that is the case, then this snippet from the VF docs may help to explain it:

 

--- snip ---

 

 

Note: If the user is redirected to a page that uses the same controller and the same or a proper subset of controller
extensions, a postback request is made.When a postback request is made, the view state is maintained.

Note: If the user is redirected to a page that uses the same controller and the same or a proper subset of controllerextensions, a postback request is made.When a postback request is made, the view state is maintained.

 

 

--- snip ---

Scott.MScott.M

Let me know if you ever figure out how to throw a 404 exception :)

JasonGablerJasonGabler

Hi Bob,

 

Yeah, if I understand what that means, I see how it explains why I had to force the issue with with the setRedirect(true).   Honestly, I read that earlier and I believe it would only make sense to me in hindsight ... as it does now :)

 

Thanks for the input,

 

jason

JasonGablerJasonGabler

Scott,

 

Coincidentally, this is precisely what I am doing... well actually I'm just creating my own faux 404.  I'm making sure the object requested is available and then I'm taking the user to a 'not found' page.

 

Are you talking about thrown a real 404?   Why not redirect the user to some oddball PageReferece than you know will never exist (e.g.  "return new PageReference('/some/fake/page');".  This will certainly throw a 404, and cause Salesforce to show your Force.com site's configured 404 page.

 

How's that?

 

jason

Scott.MScott.M

That's a great idea :) I think I'll try it.

 

Thanks!!