Tag Archives: Apex Class

Insert Record by Using Visualforce Page and Apex Class

In below example I’m inserting Account object record using Visualforce Page and Apex Class.

Visualforce Page:

<apex:page controller="CreateAccountController">
    <apex:form>
        <apex:pageblock>
            <apex:pageblocksection>
                <apex:inputfield value="{!acc.Name}"/>
                <apex:inputfield value="{!acc.Accountnumber}"/>
            </apex:pageblocksection>
            <apex:commandbutton action="{!SaveMethod}" value="Save"/>
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex Class:

public class CreateAccountController{
    
    //Prpoerties
    public Account acc {get;set;}
    
    //Constructor 
    public CreateAccountController(){        
        //Instances        
        acc = new Account();
    }
    
    //Save Method 
    public PageReference SaveMethod(){
        Insert acc;
        return null;
    }
}