0

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; 
}
}
0

Whenever you click on any <apex:commandButton>, it first processes the validation rules associated with the fields on the page i.e. in your case it is checking for mandatory fields. This is the default behavior of <apex:commandButton>.

And in your case, it is happening only in first two pages and not in third page because third page does not have any inputFields and does not cause any validation errors.

So to skip this validations, you can use immediate attribute of <apex:commandButton> which skips all the validations and performs the action associated with that button immediately.

A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.

For further help, you can refer <apex:commandButton> and this blog which explains the same thing.

So your visualforce code would be

<apex:commandButton value="Cancel" immediate="true" action="{!cancel}"></apex:commandButton>

This same thing is also mentioned in the example that you are referring i.e. in Creating a Wizard

2
  • thank you so much, that works !! @VijayGanji – sarthvader Apr 18 '20 at 17:32
  • That's great !! 👍👍 – Vijay Ganji Apr 19 '20 at 4:30

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.