In my page constructor I call a method isFirstPageLoaded, to check if the loaded page is the first page of the wizard.

 private final PageReference FIRSTPAGE = Page.Maintenance_Lock_1;

 private void isFirstPageLoaded() {
         if(ApexPages.currentPage()!=this.FIRSTPAGE) {
             throw new MaintenanceLockException(this.FIRSTPAGE + ' --- ' + ApexPages.currentPage());

The problem is: Even if I open the first page of the wizard, the exception is thrown. How can I compare two PageReferences? I would like to avoid to parse the URLs and remove parameters etc...

  • how about using OutputPanel and differentiating the wizard first page ? Feb 13, 2013 at 10:00
  • 1
    What exactly are you trying to do? The answers given are helping you with your question, but honestly it's very rare for checking the URL to be the right thing to do. Your controller should be maintaining state on its own. I usually have a Integer pageNum member that gets initialized to 1 and goes +/- as the user performs navigation through the various wizard pages.
    – jkraybill
    Feb 14, 2013 at 5:23

3 Answers 3


The == operator does a deep comparision, so the fact that one of the PageReferences has parameters and one doesn't means that they are not ==. From the expression operators documentation:

For sObjects and sObject arrays, == performs a deep check of all sObject field values before returning its result. Likewise for collections, built-in Apex types, and user-defined types.

You could do a string comparison on the URLs after stripping off the query string from the current page.

    String firstPageUrl = FIRSTPAGE.getUrl().toLowerCase();
    String currentPageUrl = ApexPages.currentPage().getUrl().toLowerCase();
    Integer qIndex = currentPageUrl.indexOf('?');
    if (qIndex >= 0) {
        currentPageUrl = currentPageUrl.substring(0, qIndex);
    System.assertEquals(firstPageUrl, currentPageUrl, 'page refs not equal');

There are other methods that would work as well, depending on the structure of your PageReference names, and are more compact (e.g., startsWithIgnoreCase). See the String documentation for more information.

    String firstPageUrl = FIRSTPAGE.getUrl();
    String currentPageUrl = ApexPages.currentPage().getUrl();
    System.assert(currentPageUrl.startsWithIgnoreCase(firstPageUrl), 'page refs not equal');

I think if you want to avoid the URL string parsing the only way to do it is to store your own custom state.

Your first page might look something like this (well call this WizPage1)

<apex:page controller="MyController" action="{!visitedFirstPage}">
   <!-- other stuff-->

Other pages like this

<apex:page controller="MyController" action="{!checkWrongStartPage}">
   <!-- other stuff-->

Your controller like this

public class MyController {

   private Boolean hasVisitedFirstPage = false; 

   public void visitedFirstPage(){
      hasVistedFirstPage = true;

    public PageReference checkWrongStartPage(){
        if (hasVisitedFirstPage) {
                return null;    
        } else {
                throw new MaintenanceLockException(...);

You could also look for some kind of data that is already stored in the view state as a result of having visited the first page to be the determining factor as to whether you push them back to the first page.

Alternatively, instead of throwing the exception, you could just helpfully push the user back to the first page of the wizard. In the "else" above, do something like this:

PageReference newRef = Page.WizPage1;
return newRef;
  • Note: this code was written free hand off of a structure I've used before, so apologies in advance if it doesn't compile. Feel free to correct!
    – pchittum
    Feb 13, 2013 at 13:48

Since != is a overloaded , probably here compiler is comparing the object address and hence reporting different value.In opinion you can compare the URL using the "getUrl" method of the Pagereference class. Something like

ApexPages.currentPage().getURL() != this.FIRSTPAGE.getURL() {
             throw new MaintenanceLockException(this.FIRSTPAGE + ' --- ' + ApexPages.currentPage());

Please note : "getUrl" also brings the url parameter you may have to remove them.

  • Thx for the suggestion, but if I have URL parameters this might not work, and I would like to avoid doing some "cleaning" operation on the URL. Feb 13, 2013 at 11:07
  • 1
    Well thats why there was a note.But I dont think there is a way to compare them without comparing the actual URL.
    – Avidev9
    Feb 13, 2013 at 12:15
  • @Avidev9 - Actually, there is an !== operator that checks the memory location (note the extra '=') Feb 13, 2013 at 17:25

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.