You can use the below code and customize it as per your need. The below controller is used on the "Case" object VF page which has the lookup to "Contact" object. Here Contact lookup has been customized to see if a case is raised from "New" button in from "Case" tab then it shows list of all contacts. If case is raised from the related list in account then only those account specific contacts are shown. If case is raised from contact then the same records gets auto-populated in lookup.
========================================
Vf Page - CustomContactLookup
<apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
<apex:pageBlock id="searchResults">
<apex:pageBlockTable value="{!results}" var="a" id="tblResults">
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Name</apex:outputPanel>
</apex:facet>
<apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:actionRegion>
</apex:outputPanel>
Apex Class - CustomContactLookupController
public with sharing class CustomContactLookupController {
public List<Contact> results{get;set;} // search results
public String searchString{get;set;} // search keyword
public String recordId;
public String CustomerId;
public CustomContactLookupController() {
// get the current search string
recordId = '';
if(System.currentPageReference().getParameters().get('recordId') != '')
recordId = System.currentPageReference().getParameters().get('recordId');
system.debug('!!!!!!!!!!!!!!recordId='+recordId);
searchString = System.currentPageReference().getParameters().get('lksrch');
system.debug('!!!!!!!!!!!!!!searchString='+searchString);
runSearch();
}
// performs the keyword search
public PageReference search() {
runSearch();
return null;
}
// prepare the query and issue the search command
private void runSearch() {
// TODO prepare query string for complex serarches & prevent injections
results = performSearch(searchString);
system.debug('!!!!!!!!!!!!!!results='+results);
}
// run the search and return the records found.
private List<Contact> performSearch(string searchString) {
String soql = 'select Id, Name from Contact';
if(recordId != '' && recordId.startsWith('001')){
if(searchString != '' && searchString != null)
soql = soql + ' where AccountId = \'' + recordId + '\' and Name LIKE \'%' + searchString +'%\'';
else
soql = soql + ' where AccountId = \'' + recordId + '\'';
} else if(recordId != '' && recordId.startsWith('003')){
if(searchString != '' && searchString != null)
soql = soql + ' where Id = \'' + recordId + '\' and Name LIKE \'%' + searchString +'%\'';
else
soql = soql + ' where Id = \'' + recordId + '\'';
} else if(recordId != '' && recordId.startsWith('a1O')){
Id accoundId = [select csord__Account__c from csord__Subscription__c where Id = :recordId].csord__Account__c;
soql = soql + ' where AccountId = \'' + accoundId + '\'';
if(searchString != '' && searchString != null)
soql = soql + ' and Name LIKE \'%' + searchString +'%\'';
}
else {
if(searchString != '' && searchString != null)
soql = soql + ' where Name LIKE \'%' + searchString +'%\'';
}
soql = soql + ' limit 25';
System.debug(soql);
return database.query(soql);
}
// used by the visualforce page to send the link to the right dom element
public string getFormTag() {
return System.currentPageReference().getParameters().get('frm');
}
// used by the visualforce page to send the link to the right dom element for the text box
public string getTextBox() {
return System.currentPageReference().getParameters().get('txt');
}}
Below is the method how to put this in Case Custom VF page. Use this javascript in your parent object VF page.
// Following "003" is the lookup type for Contact object so change this as per your standard or custom object
if(lookupType == "003"){
var urlArr = baseURL.split("&");
var txtId = '';
if(urlArr.length > 2) {
urlArr = urlArr[1].split('=');
txtId = urlArr[1];
}
// Following is the url of Custom Lookup page. You need to change that accordingly
baseURL = "/apex/CustomContactLookup?txt=" + txtId;
// Following is the id of apex:form control "myForm". You need to change that accordingly
baseURL = baseURL + "&frm=" + escapeUTF("{!$Component.myForm}");
if (modified == '1') {
baseURL = baseURL + "&lksearch=" + searchParam;
}
baseURL = baseURL + "&recordId=" + '{!recordId}';
// Following is the ID of inputField that is the lookup to be customized as custom lookup
if(txtId.indexOf('contact') > -1 ){
isCustomLookup = true;
}
}
Hope this helps you.
Thanks!