Hi,Have you gone through task fields ? There we will have a Single lookup field "WhoId" which works for both Contact as well as Leads. Thought of Implementing this Logic in VisualForce.Here we need two vf pages and controllers to do so.MultipleLookup is page 1 and controller :- Parent Page
lookuppage is page 2 and controller :- Child Page
So lets start with parent page, In this page we will have 3 fields
1)Hidden field:- To store the Id of the record(As relationship fields need ID to save)
2)Pick List :- To store Sobjects you wish to provide
3) Input text :- To display label of record selected.
Below is the
Page 1 Code : -
<apex:page controller="multiplelookup" showHeader="false">
<script> var newWin=null;
function openLookupPopupipd(name, id,val){
if(val.value != ''){
var url="/apex/lookuppage?namefield=" + name + "&idfield=" + id+"&sobject="+val.value;
newWin=window.open(url, 'Popup','height=700,width=1200,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
if (window.focus){
newWin.focus();
}
return false;
}
else{
}
}
function closeLookupPopup(){
if (null!=newWin){
newWin.close(); }
}
</script>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:outputPanel >
<apex:inputhidden value="{!hiddenval}" id="hiddenid"/><br/>
<apex:outputLabel >Field</apex:outputLabel>
<apex:selectList size="1" value="{!selectedsobject}" label="Select Sobject"
onchange="openLookupPopupipd('{!$Component.labeltoshow}','{!$Component.hiddenid}',this); return false">
<apex:selectOptions value="{!allsobjects}"></apex:selectOptions>
</apex:selectList><apex:inputText value="{!showinglabel}" id="labeltoshow"></apex:inputtext>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller 1 Code : -
public class multiplelookup {
//variable declaration
public string hiddenval{set;get;}
public string showinglabel{set;get;}
public string selectedsobject{set;get;}
//constructor
public multiplelookup (){
}
//method which gets list of sobjects
public list<SelectOption> getallsobjects(){
Map<String, Schema.SObjectType> demomap= Schema.getGlobalDescribe();
Schema.DescribeSObjectResult[] descResult =Schema.describeSObjects(new String[]{'Account','Contact','Lead','Opportunity'});
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--None--'));
for(integer i=0;i<=descResult.size()-1;i++){
DescribeSObjectResult sobjectRes = descResult[i].sObjectType.getDescribe();
system.debug('sobjects:::::'+sobjectRes );
options.add(new SelectOption(sobjectRes.getName(),sobjectRes.getName()));
}
return options;
}
}
Now Page 1 is ready.. Now lets see What is happening on selection of Sobject from Picklist. Onselection a Javascript function named "openLookupPopupipd" is invoked along with 3 parameters.
1)Hidden Field ID
2)InputText Field ID
3) Sobject we select from picklist.
Page 2 :-
Page 2 Contains list of records of sobjects we have selected from page 1.
It is a Dynamic list, and gets loaded based on sobject selected
Page 2 Code: -
<apex:page controller="lookuppage" showHeader="false">
<script language="javascript">
window.onload = new function()
{ // bring popup window to front
window.focus();
var ele=document.getElementById('{!$Component.form.block.section.query}');
if (ele) { ele.focus(); } }
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;
ele.focus();
winMain.closeLookupPopup();
}
</script>
<apex:form >
<apex:pageBlock mode="maindetail" id="show">
<apex:pageMessage severity="error" summary="No Matcing Data Found" rendered="{!showerror}"></apex:pageMessage>
<apex:pageblocksection >
<apex:pageBlockTable value="{!listofrecords}" var="rec">
<apex:column value="{!rec.id}" rendered="{!IF(Fields.size == 0 , true, false)}"/>
<!--<apex:column value="{!rec.name}" /> -->
<apex:repeat value="{!Fields}" var="FieldLable">
<apex:column rendered="{!IF(FieldLable != ' ' , true, false)}" >
<apex:outputLink value="#" onclick="fillIn('{!rec[FieldLable]}', '{!rec.id}')" >{!rec[FieldLable]}</apex:outputLink>
</apex:column>
</apex:repeat>
</apex:pageBlockTable>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller 2 Code :-
public class lookuppage {
public list<sobject> records{set;get;}
public boolean showerror{set;get;}
public List<string> fields{private set;get;}
public string selectedsobject{get;set;}
public lookuppage(){
selectedsobject = System.currentPageReference().getParameters().get('sobject');
if(selectedsobject == '' || selectedsobject ==null) {
showerror = true;
}
else{
showerror = false;
}
records = new list<sobject>();
fields = new List<string>();
}
public list<sobject> getlistofrecords(){
try{
fields = new list<string>();
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType systemObjectType = gd.get(selectedsobject);
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap();
for(Schema.SObjectField s: M.values()){
Schema.DescribeFieldResult fieldResult = s.getDescribe();
if( fieldResult.getName()=='Name'){
fields.add(fieldResult.getName());
}
}
system.debug('fields::::::::::'+fields);
String query='select Id' ;
for(string field : fields){
if(field.toLowerCase() != 'id' )
query+= ','+ field + ' ' ;
}
query+= ' from ' + selectedsobject+ ' LIMIT 100' ;
system.debug('Query:::::::::'+query);
records = Database.query(query);
system.debug('queried records::::::::::'+records);
return records;
}
catch(exception e){
return null;
}
}
}
No comments:
Post a Comment