Custom Visualforce Lookup

If you are creating a Visualforce page with a Standard controller, you can easily create Lookup field. But when using a custom controller and you have to create Lookup field, What you will do? In that type of cases, you’ll have to create the functionality of lookup by yourself. In this arcticle we will see how to create Lookup field when using custom controller. For presentation, I have developed “Add New Contact” Page with Account Lookup (text field).

Visualforce-Lookup-Code

On Click Of magnifying glass image i am calling a function of Controller which is redirecting to a new ‘Account Lookup’ VF page.

Account-Lookup-Code

And by clicking on any account name it will go back to the main page and Account Lookup text field is being populated.

Add-New-Contact

Here is the Code:

Main VF page

<apex:page controller="myController" tabStyle="Contact">
<apex:form >
<apex:actionFunction name="callAction" action="{!callLookup}"/>
<apex:sectionHeader title="Add New Contact"/>
<apex:pageBlock >
<apex:pageBlockSection title="Contact Information" columns="1">
<apex:pageMessages/>
<apex:inputText label="First Name" value="{!fName}"/>
<apex:inputText label="Last Name" value="{!lName}"/>
<apex:inputText label="Account Lookup" style="float:left;" value="{!accName}">
<apex:image value="https://api.icons8.com/download/19343120d27c16dd3e9d21ad3aa637f94fd4d5fa/Android_L/PNG/256/Very_Basic/search-256.png" height="20" onclick="callAction()"/>
</apex:inputText>
</apex:pageBlockSection>
<center>
<apex:commandButton value="Save" action="{!save}"/>
</center>
</apex:pageBlock>
</apex:form>
</apex:page>

Lookup Visualforce page
<apex:page controller="myController" tabStyle="Contact">
<apex:form>
<apex:sectionHeader title="Account Lookup"/>
<apex:pageBlock>
<apex:pageBlockSection columns="1">
<apex:repeat value="{!accList}" var="acc">
<apex:commandLink value="{!acc.Name}" action="{!goBack}">
<apex:param value="{!acc.Name}" name="AccName"/>
<apex:param value="{!acc.Id}" name="AccId"/>
</apex:commandLink>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Custom Controller

public class myController {
public String fName{get;set;}
public String lName{get;set;}
public String accName{get;set;}
public Id AccId{get;set;}
public List accList {get;set;}

public PageReference callLookup(){
accList = [Select id,Name from Account];
PageReference pr = new PageReference('/apex/AccLookup');
pr.setRedirect(false);
return pr;
}

public PageReference goBack(){
accName = ApexPages.currentPage().getParameters().get('AccName');
AccId = ApexPages.currentPage().getParameters().get('AccId');
PageReference pr = new PageReference('/apex/AddNewContact');
pr.setRedirect(false);
return pr;
}

public PageReference save(){
if(lName == null || lName == ''){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Last Name is Required!'));
return null;
}
else{
Contact con = new Contact();
con.FirstName = fName;
con.LastName = lName;
con.AccountId = AccId;
insert con;

PageReference pr = new PageReference('/'+con.Id);
pr.setRedirect(false);
return pr;
}
}
}

5 Comments
  1. Nitesh Kumar 2 years ago

    the code is missing ‘apex/AddNewContact’ ‘ not found please post complete the code or share the link where i can see the complete code

     

  2. Salesforce | Abhay
    Abhay 1 year ago

    I tried this code, it is working fine on my side.

  3. Salesforce | Abhay
    Abhay 1 year ago

    When copying VF page code from forcetalk into salesforce, sometimes inverted commas used in code automatically change to slightly different inverted commas. This could be a problem.

  4. Salesforce | Shubham Author
    Shubham 1 year ago

    I have updated the code and the issue has been fixed. Please try this updated code.

  5. Bhanu Prakash Reddy 10 months ago

    Hi

    Please update line 6 in mycontroller

    public List<Account> accList {get;set;}

    Thanks,

    Bhanu Prakash

    ForceLearn

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2020 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?