+ Start a Discussion
Sai Ram ASai Ram A 

Learning: Create Custom save method for a simple Object with VF page & Custom Controller

Hope this Helps!! Custom Save Method for an simple Object. 

Create a custom Object Books with fields
LabelName  	DataType 
Name		Text(255)	
Author		Text(255)
Email		Email
Address		Textarea(5000)

Visualforce Pagge
<apex:page controller="SaveBook">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!SaveRecord}"/>
                <apex:commandButton value="Cancel" action="{!CancelRecord}"/>                
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Book Name</apex:outputLabel>
                    <apex:inputField value="{!BK.name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Author</apex:outputLabel>
                    <apex:inputField value="{!BK.Author__c}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Email</apex:outputLabel>
                    <apex:inputField value="{!BK.Email_ID__c}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Address</apex:outputLabel>
                    <apex:inputField value="{!BK.Address__c}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

//Custom Controller
public class SaveBook {

    public SaveBook(){
        Bk = New Book__c();
    }
    public Book__c Bk {get; set;}
    
    //Save Method
    //This custom Controller method 
    // runs similar as Standard save method (Button)
    //Created Just for learning
    public PageReference Saverecord()
    {
        Book__c Book1 = new Book__c();
        Book1.Name = Bk.Name;
        Book1.Author__c = Bk.Author__c;
        Book1.Email_id__c = Bk.Email_id__c;
        Book1.Address__c = Bk.Address__c;
        insert Book1;
        return(new PageReference('/'+Book1.id).setRedirect(True));
    }
    
    //Cancel
    //Similarly works as Standard Buttons
    Public PageReference Cancelrecord(){
        return (new PageReference('/a04/o').setRedirect(True));
    }
}

 

sandeep@Salesforcesandeep@Salesforce

It is good please share problem if you are having in this.