I couldn't find this properly defined anywhere in docs on when to use System class and when to use ApexPages class to fetch current page reference. Does anyone know if there is actually any difference in using it in different situations?
2 Answers
The two are synonymous. The former is the classic means of accessing the current page, while the new method was introduced as a part of the recent normalization of functions. Other functions have also been duplicated in more appropriate spaces, such as System.Now()
vs DateTime.Now()
and System.today()
versus Date.Today()
. It is safe to use either in current code, although one would expect the classic version to be deprecated eventually. Note also that ApexPages.CurrentPage()
no longer appears in the documentation, as far as I can tell.
Proof:
Controller
public with sharing class versus {
public boolean getIsEqual() { return apexpages.currentpage() === system.currentpagereference(); }
}
Page
<apex:page controller="versus">
{!isEqual}
</apex:page>
Output
true
Conclusion
Since ===
compares two memory locations, and this operator returning true means that both functions are literally returning the same value.
-
thnx for the answers, I'm markign your as accepted one since it has POC :-) ... for my question I got curious on this because I was using System.currentpageReference() to get current page parameter, named 'id', on a VF page which has been added as a Visualforce Page to a Standard Pagelayout and it reported an Exception stating that my variable was never initialized with value (which I fetched from page parameter 'id'. But it worked second time I loaded the page.– VarunCCommented Dec 2, 2013 at 17:25
-
The error may have been elsewhere on the controller code, though. I think the question was sort of a red herring in this case, but that's okay. Either that, or you experienced a bug of some sort.– sfdcfox ♦Commented Dec 2, 2013 at 17:45
There is no difference - both System.currentPageReference() and ApexPages.currentPage() return a reference to the current page.