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
Alfredo OrnelasAlfredo Ornelas 

Save in Custom Controller

I`m  creating a visualforce page and want to know how i use SAVE action in the controller code?
The code in VFP is:

 <apex:page controller="EAccount">
 <apex:form >
      <apex:pageBlock title="NEW Email Account">
     <apex:pageblockButtons location="Bottom" >
      <apex:commandButton action="{!Save}" value="Create Account" />
   </apex:pageblockButtons>   
    <apex:pageBlockSection columns="1" title="Enter Information">
     <apex:inputField value="{!ema.First_Name__c}"/> 
     <apex:inputField value="{!ema.Last_Name__c}"/>
     <apex:inputField value="{!ema.Username__c}"/> 
     <apex:inputSecret value="{!ema.Password__c}" label="Password"/>
     <apex:inputField value="{!ema.Mobile__c}"/>
     <apex:inputField value="{!ema.Birthday__c}"/>
     <apex:inputField value="{!ema.Male__c}" label="Male"/>
     <apex:inputField value="{!ema.Female__c}" label="Female"/>            
    </apex:pageBlockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page> 

Controller Class

public class EAccount {

    public static void LoginPage(){
                           
    }
    public Email_Account__c ema{get; set;}
    public String EmailAccount { get; set; }
           
    public PageReference Save() {
     return null;    
      PageReference LoginPage = Page.MyLoginPage;
       LoginPage.setRedirect(true); 
        return LoginPage;
       } 
   }

An error occurs an is not saving the records.

bob_buzzardbob_buzzard
If you are simply inserting a record into the database, you'd do much better with a standard controller - that has a save method and you don't need to write a single line of code.

However, if you are sure you need to have a custom controller, you need to instantiate a new record in the constructor:

public EAccount(){
   ema=new Email_Account__c();                         
}


and then insert this in the save method. Also, you have two return statements in your save method so I'm surprised it compiles.  I'm assuming that you want to send the user to the login page:

public PageReference Save() {
     insert ema;
     PageReference LoginPage = Page.MyLoginPage;
     LoginPage.setRedirect(true);
     return LoginPage;
}
I'm also not sure what the point of the public static void loginPage method is...