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
Robert Davis 1Robert Davis 1 

Visualforce - Reference a Lookup Field on a Custom Object to the Contact Object

I am attempting to create a Visualforce page that allows the user to select a contact from a lookup field on a custom object to the contacts object. I am surely missing something silly. Your help would be super.
 
<apex:page standardController="Contact_Plan__c">
    <apex:pageBlock >
        <apex:pageBlockSection>
            <apex:form>
            
                <apex:inputField value="{! Contact_Plan__c.name}"/>     
                <apex:inputField value="{! Contact_Plan__r.Contact.Name}"/>
            </apex:form>
       </apex:pageBlockSection>
</apex:page>

The page would allow for the individual to enter the Contact Plan Name and then Select a Contact from the Contact Object. I have created a custom object called Contact_Plan__c.

Your help is greatly appreciated in advance.

Robert
Best Answer chosen by Robert Davis 1
badibadi
Hello Robert,

You do not need the relationship here, try this
<apex:inputField value="{! Contact_Plan__c.Contact__c}"/>
assuming Contact__c is the API name of your Contact lookup field
 

All Answers

Robert Davis 1Robert Davis 1
Here is what I came up with from a little more Googling. I hope it helps someone.

I was overthinking it. I just reference the field and Salesforce does the rest.
 
<apex:page standardController="Contact_Plan__c">
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:form>
                <apex:inputField value="{!Contact_Plan__c.contact__c}"/><br/>
                <apex:inputField value="{!Contact_Plan__c.Designation_Date__c}"/><br/>
                <apex:inputField value="{!Contact_Plan__c.Contact_Designator__c}"/><br/>
                <apex:inputField value="{!Contact_Plan__c.Designation_Plan__c}"/><br/>
                <apex:inputField value="{!Contact_Plan__c.Designation_Followup_Date__c}" />
                <apex:commandbutton value="Save & Submit" action="{!Save}"/>

                
            </apex:form>
        </apex:pageBlockSection>    
    </apex:pageBlock>   
</apex:page>

 
badibadi
Hello Robert,

You do not need the relationship here, try this
<apex:inputField value="{! Contact_Plan__c.Contact__c}"/>
assuming Contact__c is the API name of your Contact lookup field
 
This was selected as the best answer
Robert Davis 1Robert Davis 1
Thanks, Badi
dieffreidieffrei

Use component <c:AdvancedLookup> a custom visualforce component

<c:AdvancedLookup 
sObjectName="Contract" 
value="{!contractValue}" 
searchFields="ContractNumber, AccountId, Status, StartDate, EndDate, ContractTerm" formFields="AccountId, Status, StartDate, EndDate, ContractTerm" 
canCreate="true" 
fieldLabel="ContractNumber" 
onchange="onChangeContractHandler"/>

Github: https://github.com/dieffrei/visualforce-custom-lookup
Demo: https://dieffrei-com-developer-edition.na35.force.com/|

I hope you enjoy