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
Nitish Singh 22Nitish Singh 22 

Save data from VF form to Custom Object

I want to save values form a VF Page to a custom object Note__c.  How do i go about it , please suggest.

This is how my Vf Page and corresponding controller class looks like

VF Page : 

<apex:page id="notedetails" extensions="wk_NotesController" standardController="Note__c" showHeader="false" sidebar="false" standardStylesheets="false" docType="">
<apex:form id="inputform">
    <apex:outputLabel styleClass="label"   value="{!$ObjectType.Note__c.fields.Comments__c.Label}" />
    <apex:inputField id="txtcmnt" value="{!val.Comments__c}" styleClass="box"/>  
   
    <apex:outputLabel styleClass="label"   value="{!$ObjectType.Note__c.fields.Note_type__c.Label}" />
    <apex:inputField id="drpdwn" value="{!val.Note_type__c}" styleClass="box"/>  
       
    <apex:commandButton action="{!saveValues}" value="Save" id="btnSave" />
   
    </apex:form>
 
</apex:page>


Controller :

public with sharing class wk_NotesController { 

    public Note__c val{get;set;}  
  
     public String comments {
        get;
        set;
    }
   
     public String NoteType {
        get;
        set;
    }
    public wk_NotesController(ApexPages.StandardController cont){
        val= (Note__c)cont.getRecord();
    }
   
   
    public PageReference saveValues(){       
       
        return null;
    }

}
 
Abhilash Mishra 13Abhilash Mishra 13
Hi Nitish,
<apex:page id="notedetails"  standardController="Note__c" showHeader="false" sidebar="false" standardStylesheets="false" docType="">
<apex:form id="inputform">
    <apex:outputLabel styleClass="label"   value="{!$ObjectType.Note__c.fields.Comments__c.Label}" />
    <apex:inputField id="txtcmnt" value="{!Note__C.Comments__c}" styleClass="box"/>  
   
    <apex:outputLabel styleClass="label"   value="{!$ObjectType.Note__c.fields.Note_type__c.Label}" />
    <apex:inputField id="drpdwn" value="{!Note__C.Note_type__c}" styleClass="box"/>  
       
    <apex:commandButton action="{!save}" value="Save" id="btnSave" />
   
    </apex:form>
 
</apex:page>
It will save the Record and redirect you to the detail page. if you dont want to use standard Save functionality. 
add extensions in page  code of extensions class will be like this.

Please mark the Question solved it it resolve the issue by selecting it best answer.
​public with sharing class wk_NotesController { 

    public Note__c val;   
    public wk_NotesController(ApexPages.StandardController cont){
        val= (Note__c)cont.getRecord();
    }
   
   
    public PageReference saveValues(){       
       insert val;
       return  null // or pagereference of whereever you want to go now
      }

}
Abhilash Mishra 13Abhilash Mishra 13
Hi Nitish, Please change the Action Name to saveValues if you are using Extensions.