Find your content:

Search form

You are here

Is the example of creating a wizard in visualforce developer's guide wrong

 
Share

I am reading through the Visualforce developer's guide and found the example of creating a wizard might be wrong. The link is https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm .

In the controller code:

 public PageReference cancel() {
        PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
        opportunityPage.setRedirect(true);
        return opportunityPage; 
}

Although in the previous code:

 public Opportunity getOpportunity() {
      if(opportunity == null) opportunity = new Opportunity();
      return opportunity;
   }

Opportunity is a new instance, it is not inserted into database yet. Hence if we click cancel before save (that's where we should click cancel in real case), it will throw an exception. Am I understanding it correctly?

So my question here is how to make it work? By inserting a random opportunity will make this work, but that's ugly.


Attribution to: Lance Shi

Possible Suggestion/Solution #1

I'd think so. The ApexPages.StandardController.view() seems to return a PageReference with null as it's URL as demonstrated by:

System.debug( 
    (new ApexPages.StandardController(new Opportunity()).view()).getURL()
); //in log as USER_DEBUG [1]|DEBUG|null

So the assertion in the comments:

// This method cancels the wizard, and returns the user to the
// Opportunities tab

seems to be untrue, although the intention of the author is at least clear.


Attribution to: ca_peterson
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34213

My Block Status

My Block Content