16

I feel like I've noticed differences in the past between a pageReference returning null vs a void method. However, I can't recall now, does the pageReference returning null refresh the view state? Are they supposed to act identically?

1
21

Returning Null will not refresh the view state. Unless we explicitly say that setredirect(true) the view state will be maintained. An action method with no return type (void) will not reset the view state.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm

A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values. Use a PageReference object: To view or set query string parameters and values for a page To navigate the user to a different page as the result of an action method.

3
  • So are you saying there's no difference between the two? That a pageReference returning null (no setredirect=true) acts identically to a void method? – Phil B Jul 17 '13 at 18:52
  • 1
    Yes both mean same .But for Action methods we generally prefer using pagereference methods so that later it provides us the flexibility to provide navigation.While returning void will not flush the view state as well but generally later we have no flexibility to make navigation through this method – Mohith Shrivastava Jul 17 '13 at 19:01
  • 5
    You should return void if there is no possibility of a redirect. This makes it clear to other developers that may later view the source code that there will be no redirect from this action function without examining the entire function. This is useful when the function spans hundreds of lines and includes complex control structures. In the application I am working on finishing up, the prior developer always returned a null page reference instead of using void, and it significantly slowed down my progress. – sfdcfox Sep 11 '13 at 15:52

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.