+ Start a Discussion
Saranya MSaranya M 

Input field to display lookup field in Visualforce page

I have created the below Visualforce page which gets the data for the Opportunity object from the user. The Opportunity object has a field - Account name which contains a lookup to the Account object. But when I refer it through the Inputfield using {!opportunity.account.name}, it is displayed as a text box and not as a lookup field to Account object. Can someone please tell me how to modify the code? 

<apex:page controller="opptycontroller" tabStyle="Opportunity">
<apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/>
    <apex:form >
        <apex:pageBlock title="Opportunity information" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!step3}" value="Next"/>
            </apex:pageBlockButtons>
        <apex:pageBlockSection title="Opportunity products">
            <apex:inputField id="opportunityType" value="{!opportunity.type}"/>
            <apex:inputField id="opportunityLeadSource" value="{!opportunity.LeadSource}"/>
            <apex:inputField id="opportunityProbability" value="{!opportunity.Probability}"/>
           <apex:inputField id="opportunityAccountName" value="{!opportunity.account.name}"/>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by Saranya M
KeerthigeeKeerthigee
Hi Saranya,

 Every LookUp field store Id of the related object's record and its string type.

Thatswhy we use "accountid".

So,use this  <apex:inputfield value="{!opportunity.accountid}"/>.

If we want to access any lookup field in visualforce page,use id with field as mentioned above.

If you satisfy with this suggestion,please mark it as best answer


All Answers

vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
use {!opportunity.accountid} instead.
KeerthigeeKeerthigee
Hi Saranya,

 Every LookUp field store Id of the related object's record and its string type.

Thatswhy we use "accountid".

So,use this  <apex:inputfield value="{!opportunity.accountid}"/>.

If we want to access any lookup field in visualforce page,use id with field as mentioned above.

If you satisfy with this suggestion,please mark it as best answer


This was selected as the best answer
shiv@SFDCshiv@SFDC
Please Use Account Id

<apex:inputField value="{! opportunity.AccountId}"/>

If this resolve your issue. please mark answer as a solution.
Allen ManamelAllen Manamel

I am trying to do the same,

My lookup field name is Congressional_District__c.
<apex:inputField required="true" id="CongressionalRepresentative" value="{!application.Congressional_District__c}" />

application is the object in my controller

 

 

Allen ManamelAllen Manamel
I am trying to do the same,
I have a Account lookup field with the API name Congressional_District__c.
<apex:inputField required="true" id="CongressionalRepresentative" value="{!application.Congressional_District__c}" />

where application is the object in my controller

How can I achieve this?