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
Tejas Wadke 5Tejas Wadke 5 

How to get lookup fields on vosualforce using custom controller

Hi ,

I created a edit page using visualforce using custom controller but lookup fields are not visible.
How can i get over this.

Thank You.
Rohit K SethiRohit K Sethi
hi ,

you should need to use apex input field for look up field. Than that will show look up.

Thanks.
mritzimritzi
Following example Works on Contact object (Lookup Field -> AccountId )
Modify it to suit your requirement.

Apex:
public class TestClass2{
    public Contact con{get;set;}
    public TestClass2(){
        con = new Contact();
    }
    public PageReference save(){
        insert con;
        return new PageReference('/'+con.id);
    }
}
VF:
<apex:page controller="TestClass2">
    <apex:pagemessages ></apex:pagemessages>
    <apex:form >
        <apex:pageblock mode="edit">
            <apex:pageblockSection columns="2">
                <apex:inputField value="{!con.LastName}"/>
                <apex:inputfield value="{!con.AccountId}"/>
            </apex:pageblockSection>
            <apex:pageBlockButtons >
                <apex:commandbutton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>


Make sure to include all Mandatory fields

Mark this as Best Answer, if this solves your problem
Aniket KushwahaAniket Kushwaha
Thanks a lot @mritzi i am new and it took me hours to find this simple solution
Felipe PintoFelipe Pinto
interesting