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
Allen ManamelAllen Manamel 

How to display lookup field on a visual force page

I have a lookup field to Account object on "Application" object with some filter criteria. The fieldname is Congressional_District__c on Application object.

Here is my visualforce code:
<apex:inputField required="true" id="CongressionalRepresentative" value="{!CongressionalDistrict.Congressional_District__c}" />

where CongressionalDistrict is the method name in the apex class.

Here is the method in my apex class:
 public Application__c getCongressionalDistrict()
    {
        return [select Congressional_District__c from Application__c limit 1];
        
    }

This returns me a text field(instead of a lookup) which constantly displays the first value in the lookup field(instead of multiple values in lookup so that user can select a value). Attached is the screenshot:

User-added image

Also if I remove limit 1 at the end and do
return [select Congressional_District__c from Application__c];

it gives me error.

Any help would be appreciated. 
Kai Herng LauKai Herng Lau
Hi Anjli,

If my understanding is correctly, you try to display the field (Congressional_District__c) in Account object which is lookup to Application object. So I think what you need is to query the Account record instead of Application record. Can you try this code below:

Apex Class
public Account getCurrentAccount(){
        
        return [Select Id, Congressional_Distric__c from Account limit 1];
}

Visualforce 
<apex:inputField required="true" id="user1" value="{!CurrentAccount.Congressional_Distric__c}"/>

Hope this help you
 
Allen ManamelAllen Manamel
Hi Kai,

Thanks for replying back. No the 'Congressional Distrcit' field is on 'Application' object and it is a lookup field to Account.