1

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(){

    try{

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

    }
    catch(Exception e){
           System.debug(e);
    }
    }
  }

Visualforce:

<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:pageBlockSection>
         <apex:pageBlockButtons location="bottom">
                                     <apex:commandButton action="{!save}" value="Save"/>
         </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • 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 Feb 24 '14 at 18:32
  • i just need to autopopulate the firstname,lastname,phone field in vf page...according to lookup selected.... – user6934 Feb 24 '14 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 Feb 24 '14 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 Feb 25 '14 at 6:47
2

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

http://bobbuzzard.blogspot.co.uk/2011/11/retrieve-related-object-fields.html

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.

| improve this answer | |
  • How i need to do for custom object. In my case i have one custom and another is std object. – user6934 Feb 25 '14 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. – Bob Buzzard Feb 25 '14 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.