1
<apex:page controller="openPage">
<apex:form>         
   <apex:commandButton value="Open" action="{!openPage}"/> 
   </apex:form>

</apex:page>

public with sharing class openPage {
  public pageReference openPage()
    {
        pageReference pg = new pageReference('/apex/Name_Of_The_Page');
        pg.setRedirect(true);
        return pg;
    }       
}
1
  • Is it error during compiling controller class or VF page?
    – patryk
    Jun 4, 2017 at 19:18

2 Answers 2

2

Your org has a class, interface, or variable named PageReference. You'll need to reference the system class using its namespace: ApexPages.PageReference. I'd recommend that you also change that member's name, but I realize it's not always possible to refactor code (or easily, at any rate).

1

You can use the Page.PageName constructor and it will still work. Note that the default redirect value is true, so you don't have to call setRedirect(true). But even if you do, you could still one-line the whole thing.

return Page.Name_Of_The_Page.setRedirect(true);
3
  • re: avoiding setRedirect(true) -- I didn't know that -- where is this documented. All I know about is this reference and this one
    – cropredy
    Sep 29, 2017 at 1:17
  • @cropredy It's simple enough to prove: system.assertEquals(true, new PageReference('/').getRedirect());. Or more tersely: system.assert(new PageReference('/').getRedirect());
    – Adrian Larson
    Sep 29, 2017 at 2:44
  • Cool. I learned something!
    – cropredy
    Oct 1, 2017 at 4:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .