Salesforce

Custom LookUp Field in a VisualForce Page

 

Well, I have been working on a VisualForce page recently where the requirement was to create a lookup field on the Custom page.

In Salesforce the lookup field can be created by creating a relationship field between two objects and the field appears on the creation page of the child object. This field has a button that pops up whenever you need to search for related records. What if you want some changes in it like

  • It’s virtually impossible to modify your search criteria. What if you want your users to do some complex search based upon custom logic ?
  • Let’s say that a user searches for a specific related record and it doesn’t exist. To create the new record they need to close the lookup window, navigate to the tab to create the new related record, create the new record, then go back to the original record they were either editing or creating, pop up the lookup window again and find their newly created record.

A solution to such restrictions was to create your own lookup Field on the VisualForce Page using a basic VisualForce InputText tag and coding it into a Lookup field.

Here is the solution for the VisualForce Custom Lookup Field. The VisualForce Page where the custom lookup Field exist has a InputText Field to hold the name value and a InputHidden field to Hold the id of the selected Record. Suppose the related records you want to search is an object Book__c.

The Parent VisualForce Page :

 


<apex:page controller="LookUpPageController" id="page1">
<apex:form id="formid">
<apex:pageBlock title="Parent Page" id="block1">
<apex:pageBlockSection id="sec1">
<apex:pageBlockSectionItem id="item1">
<apex:outputLabel value="Book Name"/>
<apex:outputPanel >
<apex:inputHidden value="{!bookId}" id="inputid" />
<apex:inputText size="20" value="{!bookName}" id="inputname" />
<a href="#" onclick="openLookupPopup('{!$Component.inputname}', '{!$Component.inputid}');return false ">
<apex:image value="https://na15.salesforce.com/resource/1405511203000/lookupicon"/>
</a>
</apex:outputPanel>
</apex:pageBlockSectionitem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

var newWin=null;
function openLookupPopup(name,id)
{

var inid1=document.getElementById("{!$Component.page1.formid.block1.sec1.item1.inputid}").value;
var inname=document.getElementById("{!$Component.page1.formid.block1.sec1.item1.inputname}").value;
var url="/apex/lookuppage1?namefield=" + name + "&idfield=" +id+"&parentid="+inid1+"&parentname="+inname;
newWin=window.open(url, 'Popup','height=500,width=600,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
if (window.focus)
{
newWin.focus();
}

return false;
}

function closeLookupPopup()
{
if (null!=newWin)
{
newWin.close();
}
}

</apex:page>

Note: The value attribute of the apex:image tag can be given as the path to the image in the Static Resources.

The Lookup Page :


<apex:page controller="LookupPage1Controller" title="Child Page" showHeader="false">
<apex:form title="Child Page">
<apex:pageBlock title="Search">
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:inputText value="{!searchvalue}">
<apex:actionSupport event="onkeyup" action="{!onkeyupAction}" reRender="one"/>
<apex:actionSupport event="onchange" action="{!onkeyupAction}" reRender="one"/></apex:inputText>
<apex:commandButton value="Go" action="{!searchAction}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<apex:form title="Child Page" id="one">
<apex:pageBlock title="Details" rendered="{!render1}">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockTable value="{!Records}" var="record">
<apex:column headerValue="Book Name">
<apex:outputLink value="#" onclick="fillIn('{!record.Name}','{!record.id}')">{!record.Name}</apex:outputLink> </apex:column>
<apex:column value="{!record.author__c}" headerValue="Book Author"/>

<apex:column value="{!record.price__c}" headerValue="Book Price"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

function fillIn(name, id)
{
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}
var ele=winMain.document.getElementById('{!$CurrentPage.parameters.namefield}');
ele.value=name;
ele=winMain.document.getElementById('{!$CurrentPage.parameters.idfield}');
ele.value=id;
winMain.closeLookupPopup();
}

function CloseWindow()
{
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}
winMain.closeLookupPopup();
}

</apex:page>

The LookUp Page’s Controller:


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,Author__c,Price__c from Book__c where Name like :+searchvalue+'%' order by Name asc];
}else
{
render1=true;
records=[Select Name,Author__c,Price__c from Book__c 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,Author__c,Price__c from Book__c where Name like :+searchvalue+'%' order by Name asc];
if(records.isEmpty())
{
ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Error,'No Records Found'));
}
return null;
}

}

 

Thus Creating Two VisualForce Pages with its Controllers for the Solution. This solution can be evolved into having Better Search Criteria OR Filters if required.

Happy Coding.!

References:

View Complete code on Github

Advertisements

One thought on “Custom LookUp Field in a VisualForce Page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s