Custom Visualfore Lookup

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

1

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

2

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

3

Here is the Code:

Main VF page

 

 

 

 

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

0 Comments

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
Property of Forcetalks.com
Copyright 2017 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?