I have two related object Reservation(custom) and contact(standard) by lookup relationship. In my vf page i need to display the following : 1.Lookup field. 2.Firstname 3.Lastname 4.Phone 5.Booking Date/Time 6.Table for On click of save the contact gets saved with a particular reservation id. With a lookup field called Contact__c which is a lookup for the Contact object. All I want to do is upon Reservation (contact) lookup selection is to populate the firstname,lastname,phone number.I am new to salesforce.please help me to enhance my code.

EXT Controller:

public class CustomController2
public Contact contact {get; set;}
public Reservation__c reservation {get; set;}

public CustomController2(ApexPages.StandardController controller) {
    contact = new Contact();
    reservation = new Reservation__c();


public void save(){


        insert contact;
        reservation.Customer_Name__c = contact.Id; 
        insert reservation;
        //  Pagereference Pageref=new Pagereference('/apex/reservation');
     //   return Pageref;

    catch(Exception e){


<apex:page StandardController="Contact" extensions="CustomController2" >
 <apex:sectionHeader title="Reservation"/>
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Welcome">
        <apex:pageBlockSection columns="1" > 
           <apex:inputField value="{!reservation.Customer_Name__c}" />
            <apex:inputField value="{!Contact.salutation}" />
            <apex:inputField value="{!Contact.firstname}" />
            <apex:inputField value="{!Contact.lastname}" />
            <apex:inputField value="{!Contact.phone}" />   
            <apex:inputField value="{!reservation.Booking_Time_Date__c}" required="true"/>
            <apex:inputField value="{!reservation.Table_for__c}" />
         <apex:pageBlockButtons location="bottom">
                                     <apex:commandButton action="{!save}" value="Save"/>
  • Your question isn't quite clear but I want to throw out there that you can show fields on a parent object by a cross object formula field, without needed to populate text fields. Cross object formulas derive their values from the parent object (i.e. a reference, not a value on the record).
    – greenstork
    Commented Feb 24, 2014 at 18:32
  • i just need to autopopulate the firstname,lastname,phone field in vf page...according to lookup selected....
    – user6934
    Commented Feb 24, 2014 at 18:35
  • oh, I see, you would just need to rerender the section with the contact info then, when the lookup is set in the controller. You would need to set the lookup value in the controller with an actionFunction
    – greenstork
    Commented Feb 24, 2014 at 18:41
  • I am unable to get the id of the look up field....how can i get the value in controller? I am trying to get the id of lookup field which is created on my custom object using..... Reservation__c reserve = (reservation__c)stdctrl.getRecord(); reserve.contact= [select ID, firstname,lastname,phone from contact where ID =:reserve. Contact.ID limit 1];
    – user6934
    Commented Feb 25, 2014 at 6:47

1 Answer 1


I wrote a blog post explaining how to do this a couple of years ago at:


its retrieving fields from an account when a the account lookup is populated for a contact, so should be very simple to adapt to your use case.

  • How i need to do for custom object. In my case i have one custom and another is std object.
    – user6934
    Commented Feb 25, 2014 at 9:48
  • It makes no difference - you use actionsupport to submit the form, use SOQL to retrieve the related object and then output fields from that. Commented Feb 25, 2014 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .