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
Abhishek Kumar 149Abhishek Kumar 149 

Create a Visualforce form which inserts a basic Contact record

Abhishek Kumar 149Abhishek Kumar 149
Using the Visualforce apex:form component, create a page which will insert a Contact record based on First Name, Last Name and Email. After submitting the form, the user should be redirected to detail page of the new Contact record.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Abhishek,

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;
    }


   }

I hope it will be helpful.

Please mark it as best Answer if the information is informative.

Best Regards
Rahul Kumar
Ceila GarciaCeila Garcia


<apex:page standardController="Contact" Sidebar="False">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
          <apex:inputField value="{! Contact.FirstName }"/>
          <apex:inputField value="{! Contact.LastName}"/>        
          <apex:inputField value="{! Contact.Email }"/>  
            </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton action="{! save }" value="Save" />        
        </apex:pageBlockButtons>
    </apex:pageBlock>  
    </apex:form>
</apex:page>
Aravinth SambathAravinth Sambath
when i preview and insert a record in VF page, this code will not insert a contact record in my page.
i checked my whole contact
Ghazala Syeda 13Ghazala Syeda 13
<apex:page standardController="Contact">
    <apex:form>
        <apex:pageBlock title="Contact Info">
        <apex:pageBlockSection>
            <apex:inputField value="{! Contact.FirstName }"/>
            <apex:inputField value="{! Contact.LastName }"/>
            <apex:inputField value="{! Contact.Email }"/>
            <apex:commandButton action="{! save }" value="Save" />
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>