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
OnDem DevOnDem Dev 

How to create Lookup field in Visualforce?

Hi,
 
I want to create a lookup field as below using Visualforce. How can i acheive this?

 
Any help on this will be appreciated.
 
Thanks,
OnDemand
Best Answer chosen by Admin (Salesforce Developers) 
OnDem DevOnDem Dev

Hi,

I got the solution for my query.

This is what i can explain.

Lets take an Example:
Suppose you have an Employee Custom Object. In that lets say you have First Name Custom field.

Now you want the lookup icon to be displayed for the above field in your Visualforce page.
In your Controller do the following,
public Employee__c getFirstName()
{
   return [select FirstName__c from Employee__c limit 1];
}
Now, In your Visualforce page do the following.
<apex:inputField value="{!firstName.FirstName__c}"/>

where firstName is the method name in the Controller.

By doing this a lookup icon will be rendered on ur Visualforce page.

Thanks,

OnDemand



Message Edited by OnDem Dev on 10-07-2008 06:15 AM

All Answers

ShamSham
There is no standard component to generate Lookup
but if you are using <apex:inputField /> it will automatically generate lookup if you are using a reference field.
amar joshiamar joshi
now suppuse i make a custome page for lead convert

then there is one "Record Owner field then how can i display the record owner(lead owner) in that field and in standerd lead convert we can also change the record owner(lead owner) so in our VF page how we can change that name without lookup box


please provide me  solution  becoz i want to make my VF page for lead convert
OnDem DevOnDem Dev
Sham,
 
Can you please send the sample code for creating lookup field.
I will be really thankful to you.
 
Any help will be appreciated.
 
Thanks,
OnDemand
 
OnDem DevOnDem Dev

Hi,

I got the solution for my query.

This is what i can explain.

Lets take an Example:
Suppose you have an Employee Custom Object. In that lets say you have First Name Custom field.

Now you want the lookup icon to be displayed for the above field in your Visualforce page.
In your Controller do the following,
public Employee__c getFirstName()
{
   return [select FirstName__c from Employee__c limit 1];
}
Now, In your Visualforce page do the following.
<apex:inputField value="{!firstName.FirstName__c}"/>

where firstName is the method name in the Controller.

By doing this a lookup icon will be rendered on ur Visualforce page.

Thanks,

OnDemand



Message Edited by OnDem Dev on 10-07-2008 06:15 AM
This was selected as the best answer
DrawloopSupportDrawloopSupport

I'm having a problem getting my lookup icon to work. I'm using a custom controller.

 

See my post here:

 

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=12029#M12029

kprkpr

Hi OnDemDev,

 

I tried your code, but it doesn't work for me.No lookup icon is rendered and the field is just displayed as another text field.

 

Do we have anything more to do other than this code to make it work?

 

Please help,


Thanks,

Vishal_ThoriyaVishal_Thoriya

hey i am having a problem with rendering a look up icon any solutions plz...........

Thanks in advance

DrawloopSupportDrawloopSupport
Vishal_ThoriyaVishal_Thoriya

hey man how can i reference the input field

thanx for the help

Vishal_ThoriyaVishal_Thoriya

thank you all for guidance i got the solution thanks OnDem Dev and DrawloopSupport

zettahertzzettahertz

I think another gotcha is that Master Detail fields renders as a Lookup ONLY in Add/Create mode, and uneditable text in Edit mode. 

 

This is the default behavior of a master-detail field that you cannot modify it in edit mode, even in standard page layouts.

 

This tricked me a little too until i decided to try out the default behavior of this field on standard pages.

Nani44Nani44

Hi,

 

Can any one say why we need to use the input field component for a lookup. 

 

I have standard looup field on the visual force page. While creating a lookup we gave the input filed component beacuse of this cusotmers are entering the charaters into the input  field instead  of selecting the value form  lookup. By using the java scirpt i disabel the input field because of that some events are not firing in the program. so i cannot able to disable that. For this scenario is there any alternate way for us . At the end my aim is not to allow the user to enter any thing in the field

goubkgoubk

since lookup works on the real reference fields like ownerid(lookup to User), accountid (lookup to account) on contact object. 

if you are using standard controller then we can easly do this by binding the object name. lookup filed name into the input filed tag.


if you want to build dummy lookup field using custom controller please follow the below workaround.

 vf pgae :

 <apex:page controller="objectList">
   <apex:form >
  <apex:pageBlock mode="detail">
<!-- this is dummy lookup field on vf page-->
   <apex:inputField value="{!acc.ownerid}" />

<!-- passing lookup field value i.e userdid into controller methos  on button click-->
   <apex:commandButton action="{!searchdata}" title="search" value="search"/>


   </apex:pageblock>       
  </apex:form>                       
</apex:page>

 

controller :

public class objectList{

 

// creating dummy apex property
public Account acc {get;set;}

 

 // creating dummy method

   public String getAccount() {
        return null;
    }

public pagereference searchdata()
{

//you can fetch dummy lookup field vale here and process accordingly.
 System.debug('@@@@@@@@@@@'+acc.ownerid);
 return null;
}
 
  public objectList()
  {

 // instantiate your dummy object

    acc = new Account();
  }

i know i am replying very late but today i got this idea and tested it worked for me. please let me know if it works or not.

Balakrishna MandulaBalakrishna Mandula

Hey It's worked thanks a lot

Balakrishna MandulaBalakrishna Mandula

Hi On Dem Dev,

  It worked thank.

But when I try to value in look up field, record is not inserted into model. It says like " Attempt to de-reference a null object"

 

Here my code is as follows

 

<apex:outputLabel value="Parent Chatter" for="theLookup"/>
           <apex:inputField value="{!firstName.Parent_Chatter__c}"/>

 

 

public My_Cahtter__c getFirstName()
      {
           return [select id,Parent_Chatter__c from My_Cahtter__c limit 1];
       }

 

public PageReference savetherecord() {

 

My_Cahtter__c gtalk = new My_Cahtter__c();

gtalk.Parent_Chatter__c = m.Parent_Chatter__c;
    insert gtalk;
    PageReference pr = new PageREference('https://ap1.salesforce.com/a0C/o');
        return pr;
    }

 

 

could you please help me

 

Thanks in advance

 

 

 

 

thotathota

thanks its working well.

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