+ Start a Discussion

filter a lookup

Is there a way to set a filter at developement time for a lookup that the user can't change to control the list of items displayed in the lookup



Filter in lookup is not available till this version. A lot of people have suggested this.

We can wait for the  next version.




Hi, I have found a way to manage this using custom Visualforce Pages and Controller. Here the main step:


- Create a controller and define a get method for the Object you want to display in the lookup, use a select to filter the records;
- Create a custom lookup page using Visualforce;
- In your Visualforce page (the one that will contain your custom lookup fields) insert 3 field as shown below:


<apex:inputText id="Name"/>


here you will display the Name of the Object (it's a display only field for the user)



<apex:inputHidden value="{!controllerVariable}" id="hiddenId" />

here you will set the id of the Object and assign it to your controller variable;


<a title="Search" onclick="window.open('https://cs2.salesforce.com/apex/NameofYourCustomLookupPage?name={!$Component.Name(depending on your field location, you have to set the all path)}&cid={!$Component.hiddenId}','Search','width=xxx,height=xxx')">

<img class="lookupIcon" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';this.style.cursor = 'pointer';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onblur="this.className = 'lookupIcon';" alt="Search" src="/s.gif"/>


this code will display the same icon as standard input lookup field, the main idea is to open a window with your custom lookup visaulforce page adding to the URL the id of the text field (and also the hidden id field) you want to get populated.


Your custom lookup page will look something like this:


<apex:pageBlockTable value="{!YourObjectList}" var="o">
<a href="#" onclick = "top.window.opener.document.getElementById('{!$CurrentPage.parameters.name}').value='{!o.Name}'; top.window.opener.document.getElementById('{!$CurrentPage.parameters.cid}').value='{!o.Id}';
<apex:outputLabel value="{!o.Name}"/>

this will create a table with your filtered results (returned by your controller method getYourObjectList()) and when you click on an element, populate the two field (the hidden id and the input text for the user) and close the lookup custom window.


Note that if you leave the user to manually set the value, then you have to add some control code before saving (for example verify that the associated id is correct - suppose that an user set a value with your custom lookup page but then change it manually, then the id value will be wrong).


Best regards,


Message Edited by RealRevenge on 05-21-2009 08:55 AM
Message Edited by RealRevenge on 05-21-2009 08:58 AM



Can you show me your complete VF and Apex Code for Custom Lookup Filter .



@Mayank Sir,

Jairaj here...

Ye custom lookup filter implement kiya kya aapne?