ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Custom LookUp Field in a VisualForce Page

Home / Article & Blogs / Salesforce / Custom LookUp Field in a VisualForce Page

Custom LookUp Field in a VisualForce Page

By Haseeb Khan inSalesforce

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 :

 

[sourcecode language=”html”]

<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>

[/sourcecode]

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 :

[sourcecode language=”html”]

<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>

[/sourcecode]

The LookUp Page’s Controller:

[sourcecode language=”java”]

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;
}

}

[/sourcecode]

 

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

Custom LookupJavascriptLookupVisualforce Page
45
Like this post
4 Posts
Haseeb Khan

Search Posts

Archives

Categories

Recent posts

Smarter and efficient way of selecting a candidate with Compare Applicant

Smarter and efficient way of selecting a candidate with Compare Applicant

Tableau CRM: Add action buttons on Table without XMD

Tableau CRM: Add action buttons on Table without XMD

jQuery DataTable in Salesforce Lightning Component

jQuery DataTable in Salesforce Lightning Component

Boost Recruiter’s Efficiency with Smart Candidate Recommendations

Boost Recruiter’s Efficiency with Smart Candidate Recommendations

Audit Trail in Marketing Cloud

Audit Trail in Marketing Cloud

  • Previous PostSalesforce Summer '17 Release : Lightning for Gmail -Part I
  • Next PostSalesforce Summer '17 Release : Lightning Sync For Google -Part II

Related Posts

Smarter and efficient way of selecting a candidate with Compare Applicant
AppExchange ConnectEazy Salesforce

Smarter and efficient way of selecting a candidate with Compare Applicant

Tableau CRM: Add action buttons on Table without XMD
Salesforce Salesforce Einstein Tableau

Tableau CRM: Add action buttons on Table without XMD

jQuery DataTable in Salesforce Lightning Component
Jquery Lightning Salesforce

jQuery DataTable in Salesforce Lightning Component

Boost Recruiter’s Efficiency with Smart Candidate Recommendations
ConnectEazy Salesforce

Boost Recruiter’s Efficiency with Smart Candidate Recommendations

1 Comment

  1. Doug Ayers
    Reply
    24 June 2017

    Neat! I’ve often come across situation where a more relaxed search or filtering options would be more appropriate for lookups in my pages. Thanks!

    Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy