ABSYZ ABSYZ

  • Home

    Home

  • 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
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
106
Like this post
4 Posts
Haseeb Khan

Search Posts

Archives

Categories

Recent posts

How To Upload A File To Google Drive Using LWC

How To Upload A File To Google Drive Using LWC

How To  Generate A PDF Using JSPDF In LWC

How To  Generate A PDF Using JSPDF In LWC

Create a No-Code Facebook Messenger Channel in the Service Cloud

Create a No-Code Facebook Messenger Channel in the Service Cloud

Salesforce Sales Cloud Features & Pricing

Salesforce Sales Cloud Features & Pricing

Salesforce Service Cloud: What Is It? And Its Features

Salesforce Service Cloud: What Is It? And Its Features

  • Previous PostSObject Tree Structure
  • Next PostMigrating to Salesforce Files

Related Posts

How To Upload A File To Google Drive Using LWC
Integration Salesforce

How To Upload A File To Google Drive Using LWC

How To  Generate A PDF Using JSPDF In LWC
Lightning Web Components Salesforce

How To  Generate A PDF Using JSPDF In LWC

Create a No-Code Facebook Messenger Channel in the Service Cloud
Salesforce Service Cloud

Create a No-Code Facebook Messenger Channel in the Service Cloud

Salesforce Sales Cloud Features & Pricing
Sales Cloud Salesforce

Salesforce Sales Cloud Features & Pricing

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

INDIA | USA | UAE | CANADA

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

Copyright ©2022 Absyz Inc. All Rights Reserved.

Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT