I have created an Opportunity Wizard similar to this: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm Just practicing Visualforce pages. I run into an error when trying to redirect to the Home Page from the Visualforce page because the input fields are empty. Not sure why does that even matter when redirecting to a completely different page. the issue only happens on Cancel on page1 and page2, page3 redirects fine. below is my code
Page1:
<apex:page controller="OppWizardController" tabStyle="Opportunity">
</script>
<apex:sectionHeader subtitle="Step 1 of 3" title="New Customer Opportunity" />
<apex:form>
<apex:pageBlock title="CustomerInformation" mode="edit">
<apex:pageBlockSection title="Account Information">
<apex:inputField value="{!account.Name}"></apex:inputField>
<apex:inputField value="{!account.Site}"></apex:inputField>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contact Information">
<apex:inputField value="{!contact.FirstName}"></apex:inputField>
<apex:inputField value="{!contact.LastName}"></apex:inputField>
<apex:inputField value="{!contact.Phone}"></apex:inputField>
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton value="Next" action="{!toPage2}"></apex:commandButton>
<apex:commandButton value="Cancel" action="{!cancel}"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
Page2:
<apex:page controller="OppWizardController" tabStyle="Opportunity">
<apex:sectionHeader subtitle="Step 2 of 3" title="New Customer Opportunity" />
<apex:form>
<apex:pageBlock title="Opportunity Information" mode="edit">
<apex:pageBlockSection title="Opportunity Information">
<apex:inputField value="{!opportunity.Name}" />
<apex:inputField value="{!opportunity.Amount}" />
<apex:inputField value="{!opportunity.CloseDate}" />
<apex:inputField value="{!opportunity.StageName}" />
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton value="Previous" action="{!toPage1}" />
<apex:commandButton value="Next" action="{!toPage3}" />
<apex:commandButton value="Cancel" action="{!cancel} />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
Page3:
<apex:form>
<apex:pageBlock title="Confirmation Page">
<apex:pageBlockSection title="Account Information">
<apex:outputField value="{!account.name}" />
<apex:outputField value="{!account.Site}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Contact Information">
<apex:outputField value="{!contact.FirstName}" />
<apex:outputField value="{!contact.LastName}" />
<apex:outputField value="{!contact.Phone}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Opportunity Information">
<apex:outputField value="{!opportunity.Name}" />
<apex:outputField value="{!opportunity.Amount}" />
<apex:outputField value="{!opportunity.StageName}" />
<apex:outputField value="{!opportunity.CloseDate}" />
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton value="Previous"></apex:commandButton>
<apex:commandButton value="Save"></apex:commandButton>
<apex:commandButton value="Cancel" action="{!cancel}"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
Controller:
public class OppWizardController {
Account account;
Contact contact;
Opportunity opportunity;
public Account getAccount() {
if(account == null){
account = new Account();
}
return account;
}
public Contact getContact(){
if(contact == null){
contact = new Contact();
}
return contact;
}
public Opportunity getOpportunity(){
if(opportunity == null){
opportunity = new Opportunity();
}
return opportunity;
}
public PageReference toPage1(){
return Page.OppWizard1;
}
public PageReference toPage2(){
return Page.OppWizard2;
}
public PageReference toPage3(){
return Page.OppWizard3;
}
public PageReference cancel(){
PageReference pgRef = new PageReference('/lightning/page/home');
pgRef.setRedirect(true);
return pgRef;
}
}