function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sales4cesales4ce 

Cancel button not working as expected

Hi,

 

I have a visualforce page and a custom controller tied to it.The visualforce page has Account fields of which Account Name field is a required.

Also, i have a cancel button field on my page, when clicked would take the user to account tab.

 

The issue is, if i do not enter any value in the Account Name field and click cancel, it isn't working and raises a validation error to enter the acocunt name. how can i overcome with this.

 

Any help is highly appreciated.

 

Page:

 

<apex:page controller="cancelActions">
    <apex:form >
        <apex:pageBlock title="Account Information">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!acctSave}"/>
                <apex:commandButton value="Cancel" action="{!acctCancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" >
                <apex:inputField value="{!acct.Name}"/>
                <apex:inputField value="{!acct.Type}" />
            
            </apex:pageBlockSection>
        
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Apex Controller:

 

public class cancelActions {

    

    
    Public Account acct {get; set;}
    
    
    Public PageReference acctSave(){
        return Null;
    }
    
    Public PageReference acctCancel(){
        PageReference pg=New PageReference('/001/o');
        return pg;
    }
    
    
}

 



 

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
sales4cesales4ce

I figured out what the problem was. it was the attribute Immediate on the command button.

 

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

All Answers

sales4cesales4ce

I figured out what the problem was. it was the attribute Immediate on the command button.

 

<apex:commandButton value="Cancel" action="{!acctCancel}" immediate="true"/>
This was selected as the best answer
Lalit_RautelaLalit_Rautela

public PageReference doCancel(){
pageReference page = new pageReference('/apex/VF Page Name?id='+pagerecord ID);
page.setRedirect(true);
return page;
}