+ Start a Discussion
JJamesJJames 

How to create account search input field in a visualforce page

I am wondering how to create an input field like you would see on any of the pre existing SFDC edit pages for the account "Account Name Lookup (new window)" with the magnifying glass logo to the right of it, on my own custom visualforce page.  I can get the field to come up using inputField but there is no wigit on the side to search through the existing accounts.  Any help here?
Best Answer chosen by JJames
James LoghryJames Loghry
InputField is the right way to go.  What you want to do is add an apex:inputField element for the Id of the lookup.  For instance, if you're working with Contact records, you would use the AccountId field.  In the example below, I have a contact member variable, that I initialize in the extension's constructor.  If the variable is not initialized, then the lookup won't work properly.  Also, you'll want to make sure that the proper Field Level Security is set.

Given the extension controller below:
public with sharing class AccountExt {

    public Contact contact {get;set;}
    
    public AccountExt(ApexPages.StandardController sc) {
        contact = new Contact();
    }
    
}

and the page below:
 
<apex:page standardController="Account" extensions="AccountExt">
    <apex:form>
        <apex:inputField value="{!contact.AccountId}" />
    </apex:form>
</apex:page

You'll get the following:

User-added image

All Answers

James LoghryJames Loghry
InputField is the right way to go.  What you want to do is add an apex:inputField element for the Id of the lookup.  For instance, if you're working with Contact records, you would use the AccountId field.  In the example below, I have a contact member variable, that I initialize in the extension's constructor.  If the variable is not initialized, then the lookup won't work properly.  Also, you'll want to make sure that the proper Field Level Security is set.

Given the extension controller below:
public with sharing class AccountExt {

    public Contact contact {get;set;}
    
    public AccountExt(ApexPages.StandardController sc) {
        contact = new Contact();
    }
    
}

and the page below:
 
<apex:page standardController="Account" extensions="AccountExt">
    <apex:form>
        <apex:inputField value="{!contact.AccountId}" />
    </apex:form>
</apex:page

You'll get the following:

User-added image
This was selected as the best answer
JJamesJJames
Thanks, that is easy.  How would you do the same with something such as a product.  As a related list to an opportunity i would assume to create the products and assign it to the opportunity, but it is not a field from opportunity so I can't set the product object to a searchable field output. 
JJamesJJames
I just added a dummy variable called product in the opportunity which worked but, is there a better way to do this?