3

To model the situation, let's say that I have two visual force pages, which use the same controller. Let's call them PageStart and PageEnd. PageStart uses action attribute for apex:page element. Inside this action some properties are set and then PageEnd reference is returned. But when I try to display values of these properties right on PageEnd, I've got NullPointer exception, i.e. values are lost when moving from PageStart to PageEnd. It looks like new controller instance is created for PageEnd.

So, my question is: what kind of redirect happens when we return some page from apex:page action attribute's method ? Is it always external redirect even if I'm returning another page which uses exact the same controller ? Can I somehow force it to be internal redirect and keep state (set redirect to false on page ref doesn't help).

Any suggestions would be appreciated, and thanks in advance for you help guys.

  • generally we setRedirect = false to preserve controller state. Not setRedicrect(true). – Ashwani Aug 22 '15 at 18:10
  • yep, sorry that was misprint in my text, of course to false. It doesn't help in described scenario. – wesaw Aug 22 '15 at 18:27
  • can you post the method which does redirection – Ashwani Aug 22 '15 at 18:37
  • it's very simple, like this: public PageReference redirectToEnd() { x = 'test value'; PageReference pageRef = Page.PageEnd; pageRef.setRedirect(false); return PageRef; } – wesaw Aug 22 '15 at 18:43
  • If you are redirecting user as soon as it land on page the view state may not be constructed and variable may not be set. Either put the variable in <apex:form> input tag or use it as parameter in pagereference url – Ashwani Aug 22 '15 at 18:51
1

See creating a wizard for details on how it works. Basically, it works as long as both pages use the same standard controllers, custom controllers, and/or extensions, and the redirect flag is false, which actually happens to be the default. Make sure both pages are using the same set of values in the apex:page attributes.

  • Yep, I'm aware of all these stuff. I've conducted a small experiment, just copied the content of PageStart to PageEnd, so basically got two exactly same pages. Then I just added apex:outputText to PageEnd displaying property initialized on PageStart. It's still null. So external redirect happens not because of page attributes mismatching. I have a suspicious that it's always like that in the case apex:page action attribute returning non-null page. – wesaw Aug 23 '15 at 11:27
  • One more experiment: added button on PageStart which calls exactly the same action as it was for apex:page action attribute. So clicking on the button issues internal redirect to PageEnd and property is NOT NULL. So looks like the problem is not with pages, it's with redirecting using apex:page action attribute approach. I just can't find prove in official documentation that only external redirects could be triggered this way. – wesaw Aug 23 '15 at 11: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.