Saturday, 25 September 2010

As VisualForce developers know, adding an inputfield backed by a lookup or master detail field renders a field to enter a text string into and a magnifying glass icon for launching the Salesforce lookup popup. There isn't much customization possible for the lookup popup, so often there is a need to replace it with one matching your own requirement.

This blog entry shows how to create your own visualforce lookup page backed by a custom controller that gives you full control.

The Lookup link pops up the lookup window. Search text is entered into the input field and clicking the Go button returns the results into the results section:

Clicking the name populates the lookup text field on the main page, and to prove that it is working, clicking the Get Contacts button retrieves the contacts associated with the account:

The first point to mention regarding lookups is that there are actually two fields that are filled in on the target page when you select a lookup result. The name of the selected item is displayed in the visible field, but names are not unique so this is not enough information to uniquely identify the chosen item. Thus there is a hidden field that captures the the ID of the selected record.

Below is a snippet of a VisualForce page that contains my custom lookup combination field. For the sake of simplicity I've chosen to use a clickable link to launch the lookup dialog.

Digging into the markup, we can see that as well as the text field and link, the hidden field to capture the link is also present. When the user clicks the link, the following javascript is executed to open the popup. Note that the HTML element ids are passed as parameters to the popup page - this allows the popup to locate the fields to populate when the user makes a selection. Note also that the javascript function to close the popup window is located in the main page, not the popup page. This is required as browsers often only allow a window to be closed by the same page that opened it.

Now we move on to the popup VisualForce page. This consists of a criteria section and a results table. Clicking the Go button invokes a controller action method that executes a SOSL query to retrieve all accounts matching the input string. The VisualForce markup for the results table is shown below.

Can you use something like this on a sites page and have a lookup filter? The application is this: An user (role = product distributor) logs into a VF sites page, they are registering a deal (which goes into SFDC as a lead) and need to connect a partner account, which is a reseller that uses that distributor, (which is a lookup field on the lead) to the lead. Basically, the distributor is registering the deal on behalf of the reseller.

I want the distributor to only have access to look up reseller accounts that are linked to his account.

@JoAnn - I guess it could be used as a starting point for that. The controller would need some work to pull back the logged in user's information and apply a filter to the query. That said, it looks more like a partner portal use case to me - is there any reason why you wouldn't go that route?

This usually means that you have declared LookupMainController as an extension to to the standard controller for ClaimAndRequest objects. An extension controller must provide a constructor that takes the standard controller as a parameter.

Nice example. In your LookupExampleMain.page, this is a custom Visualforce page. If you wanted to "glue" the custom loookup and populate a field on a "stock" salesforce page, would your example work in this case?

got it. I was attempting to return values in the fillin method with values which were not yet completed. I was expecting the action method on the inputText to fire before onclick but it wasn't. Replaced onclick with oncomplete and it fixed it.

i used this logic in pageblock table column and its working fine. The requirement is to open the popup inline..I used the " Css display :inline" but what is happening is when i click on any of the values in column it opens the pop up inline to the first row only.. Could you please help me out. Its on an urgent basis..

I want to add 'X' next to the lookup icon which clears the input field value. After the field gets populated, later when I open the visualforce page, the field is prepopulated with that value. When I click X, the input field should be nulled. How is it possible?

Hi bob, If there is possible to call the visualfore page from the default layout of th object. i.e my requirement is i want to call the poopup page from the standard page lookup field instead of main page.

My requirement is simular. They want to replace the lookup field with a visualforce page. I have built the visulforce page and got it to search. But now am stuck on replacing the field. I am rebuilding the standard page into a visual force page to do this. I am trying to use an extension for my popup controller but am lost. Such a newbie it is sad..

Yes Bob ! I did realize that later and your 100% right about ur work being original. Also its possible for two people to write a blog post the same but the reason I pointed out was the code snippet was exactly the same :-) I will right away post on Jeff's blog to provide you the credits !

Yes Bob ! I did realize that later and your 100% right about ur work being original. Also its possible for two people to write a blog post the same but the reason I pointed out was the code snippet was exactly the same :-) I will right away post on Jeff's blog to provide you the credits !

Hi Bob, I've been trying to figure out how to pass the value of the input text name to the OpenLookupPopup with no luck. It works great without it, but I know my users will attempt to enter text then click the search button. Seems the value of the text is not set at the time the pop up is called. Your wizardry is much appreciated. Thanks!

You'll need to pull the value of the underlying HTML element and pass that through as an additional parameter to the popup URL, then get the popup controller to execute a search if the parameter has a value.

Hi Bob,Your post is quite impressive. I have a li'l similar requirement to this. In a visualforce page I have to display Account lookup. But when user don't find the record he is looking for, I need to capture the text entered by him in that field and create an Account from the controller. Is it possible to click a button by just entering a text in the lookup field even if no record exist matching with the text entered? Please guide me. TIA.

Thank You for the Post. I have similar requirement where I have 2 lookups and based on first lookup value i need to go and retrieve the 2nd lookup values. Please let me know your thoughts how to achieve it.

Once account name is populated in main page, I want to go and fetch some more fields from account object and show them in the same age. I tried action support , but it is not firing onchange event. do you have any suggestion.