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
Bill WoodsonBill Woodson 

visualforce auto fill based on lookup result

I want to present a summary of a child object on the VF screen of the parent.

On the parent I select the child using a lookup type input field and up on selection I want to show properties of the child on the screen in a summary div.

Any idea on how to do this?
KevinPKevinP
Bill,

Investigate the Rerender attribute of the apex tag outputPanel. 

Using the outputPanel, along with re-render you can set it up to update the contents of the panel (essentially a div or span tag) when an action occurs. Your panel should display the details you want to display, and have it setup so that it's re-rendered whenever your lookupfield changes.
Vance KesslerVance Kessler
I am working with Bill on this.  We already tried that and the panel is not updated.  I had a status label hooked up to the output panel and it was saying it was called and completed, but nothing changed.  Here is the code:

<apex:page standardController="Booking__c" extensions="BookingExtensionController">
<h1>Booking for {!theBooking.Opportunity__r.Name} on {!theBooking.Opportunity__r.CloseDate}</h1>
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection title="Customer Information" columns="2">
              <apex:pageBlockSectionItem >
                  <apex:inputField id="contact" value="{!theBooking.Contact__c}">
                      <apex:actionSupport event="oncomplete"
                          rerender="customerInfo" />
                  </apex:inputField>
              </apex:pageBlockSectionItem>
              <apex:pageBlockSectionItem >
                  <apex:outputPanel id="customerInfo" rendered="true">
                      <h1>Provider Info</h1><br/>
                      {!theBooking.Contact__r.Name}<br/>
                      {!theBooking.Contact__r.MailingStreet}<br/>
                      {!theBooking.Contact__r.MailingCity}, {!theBooking.Contact__r.MailingState}  {!theBooking.Contact__r.MailingPostalCode}
                  </apex:outputPanel>
              </apex:pageBlockSectionItem>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>


Public Class BookingExtensionController{
        
    public Booking__c theBooking { get; set;}
    private Apexpages.standardcontroller myController;
    
    Public BookingExtensionController(Apexpages.standardcontroller controller){
        myController = controller;
        
        List<String> fields = new List<String>();
        fields.add('Contact__c');
        fields.add('Contact__r');
        fields.add('Contact__r.Name');
        fields.add('Contact__r.MailingStreet');
        fields.add('Contact__r.MailingCity');
        fields.add('Contact__r.MailingState');
        fields.add('Contact__r.MailingPostalCode');
        fields.add('Opportunity__r');
        fields.add('Opportunity__r.Name');
        fields.add('Opportunity__r.CloseDate');
        myController.addFields(fields);
        
        this.theBooking = (Booking__c)myController.getRecord();
    }
}
Vance KesslerVance Kessler
I have been working on this and still have not solved it.  I have read MANY posts and I am at least to the point where my controller method is being called but I cannot get the ID of the contact selected to come through so I can load that contact.  I have completely rewritten this to start at the Opportunity level and here is the code.  WHAT AM I MISSING????


I have a custom Booking object that has a Contact field on it that is a lookup type to Contact.


<apex:page standardController="Opportunity" extensions="OpportunityExtension">
    <h1>Create booking for {!opportunity.Name} on {!opportunity.CloseDate}</h1>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Provider Information">
                <apex:actionRegion >
                    <apex:inputField value="{!theBooking.Contact__c}">
                        <apex:actionSupport event="onchange" action="{!updateContactInfo}" status="queryStatus" reRender="providerInfo">
                            <apex:param name="selectedContactId" value="{!theBooking.Contact__r.Id}" />
                        </apex:actionSupport>
                    </apex:inputField>
                </apex:actionRegion>
                <apex:outputPanel id="providerInfo">
                    Name: {!theContact.Name}<br/>
                </apex:outputPanel>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Dates">
                <apex:inputField value="{!theBooking.StartDate__c}"/>
                <apex:inputField value="{!theBooking.EndDate__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <apex:actionStatus id="queryStatus" startText="(Updating...)" stopText="(done)" />
    </apex:form>
</apex:page>


Public class OpportunityExtension
{
    public Booking__c theBooking { get; set;}
    public Contact theContact { get; set;}
    private Apexpages.standardcontroller myController;
    
    Public OpportunityExtension(Apexpages.standardcontroller controller){
        myController = controller;
        theBooking = new Booking__c();
    }
    
    Public void updateContactInfo() {
        String id = Apexpages.currentPage().getParameters().get('selectedContactId');
        system.debug('in updateContactInfo and about to load' + id);
        theContact = [SELECT Name FROM Contact WHERE Id = : id];
    }
}
Vance KesslerVance Kessler
fyi, I have also tried creating a property on the controller and using assignTo in the apex:param line but it was never set either.  I prefer NOT creating properties on my controller simply to pass method parameters.
Vance KesslerVance Kessler
P.S. fyi, I just tried entering a hard-coded contact ID and it worked just fine.  So my entire problem is getting the currently selected contact ID of the contact that was just selected in the search dialog.
Vance KesslerVance Kessler
I FIGURED IT OUT!!!  This works to update the Contact name field when a contact is selected.  Now to add more fields.

<apex:page standardController="Opportunity" extensions="OpportunityExtension">
    <h1>Create booking for {!opportunity.Name} on {!opportunity.CloseDate}</h1>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Provider Information">
                <apex:actionRegion >
                    <apex:inputField value="{!theBooking.Contact__c}">
                        <apex:actionSupport event="onchange" action="{!updateContactInfo}" status="queryStatus" reRender="providerInfo" />
                    </apex:inputField>
                </apex:actionRegion>
                <apex:outputPanel id="providerInfo">
                    Name: {!theContact.Name}<br/>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
      <apex:actionFunction name="reQuery" action="{!updateContactInfo}" onComplete="rerenderprovider();">
          <apex:param name="contactId" value="{!theBooking.Contact__r.Id}" assignTo="{!selectedContactId}"/>
      </apex:actionFunction>
      <apex:actionFunction name="rerenderprovider" reRender="providerInfo" />
      <apex:actionStatus id="queryStatus" startText="(Updating...)" stopText="(done)" />
    </apex:form>
</apex:page>


Public class OpportunityExtension
{
    public Booking__c theBooking { get; set;}
    public Contact theContact { get; set;}
    private Apexpages.standardcontroller myController;
    
    Public OpportunityExtension(Apexpages.standardcontroller controller){
        myController = controller;
        theBooking = new Booking__c();
    }
    
    Public PageReference updateContactInfo() {
        theContact = [SELECT Name FROM Contact WHERE Id = :theBooking.Contact__c];
        return null;
    }
}