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
Miranda L 2Miranda L 2 

How to filter visualforce lookup field

Hello there,
We have a visualforce page where we use lookup field there all the record types showing but I want it show only trade record type contract owner.
Thanks
Abhishek BansalAbhishek Bansal
Hi Miranda,

There are several option to achieve this as mentioned below:
  1. If you want the same thing to happen on all the pages where this lookup is used than simply create a lookup filter on this field. Go to Setup -> Your Object -> Fields Section -> Go to this field -> Click Edit -> Scroll down to filter section -> Add lookup filter -> Chose the record type filter as the Record Type whose records who you want to show.
  2. If this lookup field is also used on other pages where you dont want this thing to happen so you can create one more field with the same lookup on your object and on this new field add the lookup filter as described in #1. You can now use this new field in your page which has a lookup filter.
  3. If both the options are not good to go then the last option would be to customize the lookup functionality in your VF page and maintain it yourself to only pick the certain record type records. You can use this link to construct your own custom lookup.
Let me know if you need any more information or help on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
Miranda L 2Miranda L 2
Hi Abhishek,
thank you for your reply, can you please post the link for 
You can use this link to construct your own custom lookup.
Thank you
Abhishek BansalAbhishek Bansal
I already did, didn't you get it?
Miranda L 2Miranda L 2
Thank for sharing link,
I tried your given link solution but I am not looking like this I want to put filter which will show only specific ecord type Account records. like 
User-added image
if I click on this lookup then only Trade Account record type record should display. How could I do that.
Thanks
Abhishek BansalAbhishek Bansal
Hi Miranda,

If you have used the above than you should have created a class where yu are doing SOQL on the Account object, in this SOQL you need to add one filter in the where clause so that it will return only Trade record type accounts.
Your class should use SOQL like this:
public with sharing class LookupPage1Controller {
 
public Boolean render1 { get; set; }
 
List<Book__c> records=new List<Book__c>();
 
public String searchvalue { get; set; }
 
public LookupPage1Controller()
{
try
{
searchvalue=ApexPages.currentPage().getParameters().get('parentname');
String id=ApexPages.currentPage().getParameters().get('parentid');
 
if(String.IsNotBlank(searchvalue)){
render1=true;
records=[Select Name from Account where Name like :+searchvalue+'%' AND RecordType.Name = 'Trade' order by Name asc];
}else
{
render1=true;
records=[Select Name from Account where RecordType.Name = 'Trade' order by Name asc];
 
}
}catch(Exception e)
{
}
}
 
public List<Book__c> getRecords() {
if(records.size()!=0)
{
return records;
}else
{
return null;
}
}
 
public PageReference onkeyupAction() {
searchAction();
return null;
}
 
public PageReference searchAction() {
render1=true;
records=[Select Name from Account where Name like :+searchvalue+'%' AND RecordType.Name = 'Trade' order by Name asc];
if(records.isEmpty())
{
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Error,'No Records Found'));
}
return null;
}
 
}

Let me know if you have other doubts.

Thanks,
Abhishek Bansal.