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
nduchnduch 

How to create Lookup field in Visualforce - solution found

Hi,

I know that there has already been many discussions about this, but I still could not find a working solution. However, I got it to work, so I would like to share my solution with you.

 

To create a lookup field in Visualforce, you need <apex:inputField> wrapped in <apex:form>.

The value attribute in <apex:inputField> must reference a real Lookup field. For example, in Contacts there is a standard Lookup field called ReportsTo.

 

Here is my Visualforce code:

 

<apex:page standardController="Contact" extensions="SendEmailPageController">

            <apex:form>
                  <apex:inputField value="{!person.ReportsToId}"/>
            </apex:form>

 

Here is the Apex controller (I am overriding a standard controller for a Contact object):

 

public class SendEmailPageController {
    public Contact person {set;}

    public Contact getPerson()
    {
       return [select id, reportstoid from Contact limit 1];
    }

}

 

Hope it helps!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I'm slightly confused as to how this is creating a lookup field in visualforce - this looks like its using the standard inputfield component.   You could do this using entirely standard functionality:

 

 

<apex:page standardController="Contact">
    <apex:form>
          <apex:inputField value="{!Contact.ReportsToId}"/>
    </apex:form>
 

 

All Answers

Imran MohammedImran Mohammed

Good One.

bob_buzzardbob_buzzard

I'm slightly confused as to how this is creating a lookup field in visualforce - this looks like its using the standard inputfield component.   You could do this using entirely standard functionality:

 

 

<apex:page standardController="Contact">
    <apex:form>
          <apex:inputField value="{!Contact.ReportsToId}"/>
    </apex:form>
 

 

This was selected as the best answer
nduchnduch

HI Bob,

your solution also works AND is much shorter.

 

Many thanks!