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

add contact with custom page and controller

I have an custom page that shows come contacts with inline edit option.


I also have another apex:pageBlock 

that contains add new contact fields. the contact can be added successfuly.


But the problems are below.

1)If user clicks on any navigation link.. error comes lastname cannot be empty i know it is required.. but what if user do not want to add another contact.

2)I want to show and hide this on user click pageBlock.. i have added Jquery but i am not able to find a suitable selector for ti.


Here is the controller.

public class approvestep2companycontact{

public PageReference saveNew() {
newContact.AccountId = opportunity.Account.Id;
insert newContact;

PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
return pageRef;

public PageReference back(){
PageReference oPageRef = Page.ApproveStep1CompanyInfo;
oPageRef .getParameters().put('id', );
return oPageRef;

public PageReference save() {
update contacts;
return null;
public PageReference approve() {
update contacts;
PageReference oPageRef = Page.ApproveStep2CompanyContact;
oPageRef .getParameters().put('id', );
return oPageRef;

public PageReference edit() {
return null;

Public List<Contact> contacts {get;set;}
private final Opportunity opportunity;
private final Contact newContact;

public approvestep2companycontact() {
opportunity = [SELECT Id,Account.Id, Name FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id') ];
contacts = [SELECT LastName, FirstName FROM contact WHERE AccountId = :opportunity.Account.Id ];
newContact = new Contact();

public Contact getNewContact(){
return newContact;

public List<Contact> getContacts () {
return contacts ;


Here is the vf page.

<apex:page controller="approvestep2companycontact" >
<apex:includeScript value="" />
<apex:form >
<apex:pageBlock title="Company Contacts" mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" id="saveButton" value="Save"/>
<apex:commandButton action="{!approve}" id="approveButton" value="Approve"/>
<apex:commandButton action="{!back}" id="bcakButton" value="Back"/>
<apex:pageMessages />
<apex:pageBlockTable value="{!contacts }" var="item">
<apex:column headerValue="Last Name">
<apex:outputField value="{!item.LastName}"/>
<apex:column headerValue="First Name">
<apex:outputField value="{!item.FirstName}"/>
<apex:pageBlock title="Add New Contact" id="pbNewContact">
<apex:pageBlockButtons >
<apex:commandButton action="{!saveNew}" id="saveButton" value="Save"/>
<apex:pageBlockSection >
<apex:inputField value="{!newContact.Firstname}"/>
<apex:inputField value="{!newContact.Lastname}"/>
<apex:inputField value="{!newContact.Title}"/>
<apex:inputField value="{!newContact.Email}"/>
<apex:inputField value="{!newContact.Fax}"/>
<apex:inputField value="{!newContact.Phone}"/>
<apex:inputField value="{!newContact.OtherPhone}"/>

<script type="text/javascript">
j$ = jQuery.noConflict();
j$(document).ready(function() {






Use immediate="true" for bypassing the Validation.


For showing/hiding pageblock following url will help u..