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
Manvendra Singh Rathore 22Manvendra Singh Rathore 22 

Stop standard validations on custom cancel button in custom controller

I have impletemened account insert page, with custom controller. 
Here is the code for custom controller : 

public class AccountInsertPageCustomCtrl {
    public Account acc{get; set;}
    
    public  AccountInsertPageCustomCtrl(){
        acc = new Account();
    }
    
    public PageReference Save(){
        if(acc != null){
            insert acc;
        }
        PageReference pg = new PageReference('/'+acc.Id);
        pg.setRedirect(true);
        return pg;
    }
    public PageReference SaveAndNew(){
        if(acc != null){
            insert acc;
        }
        PageReference pg = new PageReference('/001/e?retURL=%2F0016F00002CaaO4');
        pg.setRedirect(true);
        return pg;
    }
    public PageReference Cancel(){
        
        //PageReference pg = new PageReference('/001/o');
        //pg.setRedirect(true);
        return null;
    }
}

Scenario : when I open the VF Enter the required fields, and without clicking on Save and Save&New button, if I click on Cancel, its working fine. as per I have mentioned in the code. but when I do not enter anything, its showing me validation error for Account name ? how can I make it work? I mean, on clicking on Cancel, it must redirect to the All accounts page. 
Best Answer chosen by Manvendra Singh Rathore 22
Nisar799Nisar799
Hi Manvendra Singh Rathore,

On your VF page, <apex:commandbutton tag use "immediate" attribute, and mark its value to true.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_commandButton.htm

Best Regards
Nisar
Happy Coding :)

All Answers

Nisar799Nisar799
Hi Manvendra Singh Rathore,

On your VF page, <apex:commandbutton tag use "immediate" attribute, and mark its value to true.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_commandButton.htm

Best Regards
Nisar
Happy Coding :)
This was selected as the best answer
Manvendra Singh Rathore 22Manvendra Singh Rathore 22
Thanks so much... @nisar799 It worked... :)