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
ashish jadhav 9ashish jadhav 9 

How can I make a simple vf page that accept user input and insert into custom object?

I'm new to salesforce and would like to create a sample vf page which accept user input and once I clicked on save it will get inserted into my custom object and I want another button to show the record of the custom object on vf page, can you please help me for the code of vf and controller.
Its just for my learning purpose.
Best Answer chosen by ashish jadhav 9
JyothsnaJyothsna (Salesforce Developers) 
Hi ashish,

Please check the below sample code.
Visualforce page:
<apex:page Controller="ContactController" >
    <apex:form >
        
        <apex:pageBlock title="Edit Contact">

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!c.FirstName}"/>
                <apex:inputField value="{!c.LastName}"/>
                <apex:inputField value="{!c.Email}"/>
                <apex:inputField value="{!c.Birthdate}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
          <apex:pageBlockTable value="{!samepage}" var="c">
          <apex:column headerValue="First Name">
          <apex:outputField value="{!c.Firstname}"/>
          </apex:column>
          
          <apex:column headerValue="Last Name">
          <apex:outputField value="{!c.Lastname}"/>
          </apex:column>
          
          <apex:column headerValue="Birthdate">
          <apex:outputField value="{!c.Birthdate}"/>
          </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller:
 
public with sharing class ContactController {

    public Contact c { get; set; }

    public List<Contact> samepage { get; set; }
    
    public ContactController(){
       c=new Contact();
    }

    public PageReference save() {
       insert c;  
      samepage= [select id,FirstName,LastName,Email,Birthdate from Contact where id=:c.id];
      
        return null;
    }


   }

screenshot:

User-added image

Hope this helps you!
Best Regards,
Jyothsna

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Ashish,

There are to many sample code are available in Google but i will recomand you please check  below trailhead module which will help you alot in learning VF page
1) https://developer.salesforce.com/trailhead/module/visualforce_fundamentals

Sample code for
<apex:page standardController="Contact" >
    <apex:form >
        
        <apex:pageBlock title="Edit Contact">

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
                <apex:inputField value="{!Contact.Email}"/>
                <apex:inputField value="{!Contact.Birthdate}"/>
            </apex:pageBlockSection>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>

        </apex:pageBlock>
        
    </apex:form>
</apex:page>
Let us know if this will help you. Above sample is for contact object just replace that with custom object.

Thanks
Amit Chaudhary


 
Sagar PareekSagar Pareek
Best resource to go thru - https://developer.salesforce.com/page/Building_Visualforce_Pages_Using_the_Standard_Controller
ashish jadhav 9ashish jadhav 9
Thank you so much Amit, can you please tell me how can I insert this values into object using controller? any sample class?
JyothsnaJyothsna (Salesforce Developers) 
Hi ashish,

Please check the below sample code.
Visualforce page:
<apex:page Controller="ContactController" >
    <apex:form >
        
        <apex:pageBlock title="Edit Contact">

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!c.FirstName}"/>
                <apex:inputField value="{!c.LastName}"/>
                <apex:inputField value="{!c.Email}"/>
                <apex:inputField value="{!c.Birthdate}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
          <apex:pageBlockTable value="{!samepage}" var="c">
          <apex:column headerValue="First Name">
          <apex:outputField value="{!c.Firstname}"/>
          </apex:column>
          
          <apex:column headerValue="Last Name">
          <apex:outputField value="{!c.Lastname}"/>
          </apex:column>
          
          <apex:column headerValue="Birthdate">
          <apex:outputField value="{!c.Birthdate}"/>
          </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Controller:
 
public with sharing class ContactController {

    public Contact c { get; set; }

    public List<Contact> samepage { get; set; }
    
    public ContactController(){
       c=new Contact();
    }

    public PageReference save() {
       insert c;  
      samepage= [select id,FirstName,LastName,Email,Birthdate from Contact where id=:c.id];
      
        return null;
    }


   }

screenshot:

User-added image

Hope this helps you!
Best Regards,
Jyothsna
This was selected as the best answer
Abu HashimAbu Hashim

@Ashish, // U can call Save method from ur vf page, which will save the record.

public class MyExtensionClass {
    public ApexPages.StandardController sc;
    public Contact ct{get;set;}
    public MyExtensionClass(ApexPages.StandardController sc) {
        this.sc = sc;
        Contact c = (Contact) sc.getRecord();
    }
    public PageReference save() {
        return sc.save();
    }
    
}
Hope it helps!!

Amit Chaudhary 8Amit Chaudhary 8
Hi Ashish,

Please check below post for for controller. I hope that will help you
1) https://developer.salesforce.com/trailhead/visualforce_fundamentals/visualforce_custom_controllers

Please try below code
<apex:page standardController="case" extensions="CloneCaseController">

   <apex:form id="theForm">
      <apex:pageBlock id="pb">
        <apex:messages ></apex:messages>
        <apex:pageBlockSection columns="1" title="Clones objcase" id="pbs_Clone">
                <apex:inputField value="{!objcase.OwnerId}" required="false"/>
                <apex:inputField value="{!objcase.AccountId}" required="true"/>
                <apex:inputField value="{!objcase.caseNumber}" required="false"/>
                <apex:inputField value="{!objcase.Status}" required="true"/>
        </apex:pageBlockSection>

        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" />
            <apex:commandButton value="Cancel" action="{!Cancel}" />
         </apex:pageBlockButtons> 
         
      </apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class CloneCaseController {

    public Case objcase {get;set;}        
    public string caseID;                        
   
    private Case objNewcase;
    private string queryString = '';
    public string strPrevCurrency {get;set;}
    Map<String, Schema.SObjectField> mapcaseFields;

    public CloneCaseController(ApexPages.StandardController controller) 
    {
        objcase = (case) controller.getRecord();
        caseID = ApexPages.currentPage().getParameters().get('id');
        if(caseID != null)
        { 
                mapcaseFields = Schema.SObjectType.case.fields.getMap() ;
                objNewcase = [ select id,OwnerId,AccountId,caseNumber,Status from case where id = :caseID ] ;
                objcase = objNewcase.clone(false,true,false,false);
        }    
    }
    
    
    public PageReference save()
    {
        insert objcase;
        
        return new PageReference('/'+objcase.id);
    }
    
}

Let us know if this will help you

Thank's
Amit Chaudhary
Nidhi MeshramNidhi Meshram
but if we insert another record it does not work it throws exceptions 
 
Prathamesh JadhavPrathamesh Jadhav
How can I make a simple vf page that accept user input for department and shows related contacts of that department
Ganesh GayakwadGanesh Gayakwad
Hello Amit,

I have followed your steps but I am getting this error ("Error: Unsupported attribute vlaue in <apex:inputField> in CreateContact at line 8 column 71").
Thanks,
Ganesh Gayakwad