21

How is returning null different from returning ApexPages.currentPage() as a result of a controller action?

13

The major difference is when you return null the constructors defined in the controller class don't execute.

While if you return whole page the page gets refreshed and constructors get executed only if you have made setRedirect flag set to true.Hence we have an option either to flushout the viewstate or not using this approach.Returning ApexPages.currentPage() will provide us ability to choose set redirect as true or false depending on the business need for the page.

If you are using reRender tags for ajax actions then we prefer null as to avoid whole refresh of page .

  • 8
    Returning the current page won't run the constructors unless the setRedirect flag is set to true though. From the docs "If the postback request indicates a page redirect and the redirect is to a page that uses the same controller and a proper subset of controller extensions of the originating page, a postback request is executed for that page." In this case, the same controller would be in use as its the same page. – Bob Buzzard Apr 12 '13 at 14:20
  • 1
    Thanks i just verified for both scenario using return null and also using return ApexPages.currentPage().You are Perfect!!!Constructors wont run unless we have setredirect set to true .I will include your points too in answer and modify my answer .Using ApexPages.CurrentPage we will have an option to run the constructor everytime .Thanks again for removing the myth i had on this concept – Mohith Shrivastava Apr 12 '13 at 19:13
  • Oh God! You'll never know how much helpful this for me!!! – highfive Oct 28 '15 at 5:05

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.