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
Srinivas AnnamSrinivas Annam 

visual force creation

from vf page we can implement three fields firstname,lastname,designation and one submit button.
whenever we click submit button the values will store in object as well as display below to the fields as records.how can we achieve this through coding.
Best Answer chosen by Srinivas Annam
JyothsnaJyothsna (Salesforce Developers) 
Hi Srinivas,

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 location="bottom">
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
          <apex:pageBlockTable value="{!samepage}" var="c" rendered="{!showblock}">
          <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 Boolean showblock { get; set; }

    public Contact c { get; set; }

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

    public PageReference save() {
       insert c;  
       showblock=true;
      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

JyothsnaJyothsna (Salesforce Developers) 
Hi Srinivas,

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 location="bottom">
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
          <apex:pageBlockTable value="{!samepage}" var="c" rendered="{!showblock}">
          <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 Boolean showblock { get; set; }

    public Contact c { get; set; }

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

    public PageReference save() {
       insert c;  
       showblock=true;
      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
Srinivas AnnamSrinivas Annam
thank you total code ok but,after save... the fields will refresh...