+ Start a Discussion
Sangeetha TSangeetha T 

VF page to display existing records and create new records

Hi All

I have created a VF page to create a new contact and a button to show the list of  existing contacts

But when i click show the error "required fields"

How to have both command buttons on one single page 
When i click show it should show list of contacts 
<apex:page standardcontroller="Contact" extensions="contactslist">
<apex:form >
<apex:pageBlock title="Create a new contact">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Enter the details for creating a new contact" columns="2">
<apex:inputField value="{!contact.Lastname}" />
<apex:inputField value="{!contact.email}" required="true"/>
<apex:inputField value="{!contact.phone}" required="true"/>
<apex:inputField value="{!contact.AccountId}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="List of contacts and Accounts">
<apex:pageBlockTable value="{!conli}" var="a">
<apex:column title="lastname" value="{!a.lastname}"/>
<apex:column title="email" value="{!a.email}"/>
<apex:column title="phone" value="{!a.phone}"/>
<apex:column title="Account_name" value="{!a.Account.name}"/>
<apex:column title="Account.Type" value="{!a.Account.Type}"/>
<apex:column title="Account.Industry" value="{!a.Account.Industry}"/>
</apex:pageBlockTable>
<Apex:commandButton value="show" action="{!show}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
 
public with sharing class contactslist
{

    public contactslist(ApexPages.StandardController controller) {

    }

public list<contact> conli {get; set;}
public pageReference show()
{
conli=[select lastname,Email, phone, Account.name, Account.Type, Account.Industry from contact where Account.name != Null order by Contact.CreatedDate];
return null;
}
}

 
Best Answer chosen by Sangeetha T
Vivek DVivek D
Hi Sangeetha,
Add the missing <apex:form> tag,  command button submits the current form which they are part of, you have included both the section in one form so when you click Show button it submits the full form with the input section also. modify the page code as
<apex:page standardcontroller="Contact" extensions="contactslist">
<apex:form id="myform" >
<apex:pageBlock title="Create a new contact">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" Rerender="myform"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Enter the details for creating a new contact" columns="2">
<apex:inputField value="{!contact.Lastname}" />
<apex:inputField value="{!contact.email}" required="true"/>
<apex:inputField value="{!contact.phone}" required="true"/>
<apex:inputField value="{!contact.AccountId}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<!-- New From Tag -->
<apex:form id="displayForm" >
<apex:pageBlock title="List of contacts and Accounts">
<apex:pageBlockTable value="{!conli}" var="a">
<apex:column title="lastname" value="{!a.lastname}"/>
<apex:column title="email" value="{!a.email}"/>
<apex:column title="phone" value="{!a.phone}"/>
<apex:column title="Account_name" value="{!a.Account.name}"/>
<apex:column title="Account.Type" value="{!a.Account.Type}"/>
<apex:column title="Account.Industry" value="{!a.Account.Industry}"/>
</apex:pageBlockTable>
<Apex:commandButton value="show" action="{!show}" Rerender="displayForm"/>
</apex:pageBlock>
</apex:form>
</apex:page>