+ Start a Discussion
vinothvinoth 

Multi select Lookup value

Hi,

 

I need to create multi select lookup. Once i click the lookup icon, one window should open with list of values and allow the user to select more than one value by clicking checkbox nearer to that value. Once After clicking “done” button, all the selected values should reflect in relevant lookup field in parent window. I am having the coding for single selection, but i need for multiple selected values.

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

<apex:page controller="DescribeSobjectTest" showHeader="false" sidebar="false" id="pageid">
<script>
body .bPageBlock {
border-top-width: 0px;
margin-bottom: 10px;
padding: 0;
}
</script>
<script src="/soap/ajax/16.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/16.0/apex.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/functions.js"></script>
<apex:form id="formid">
<apex:outputPanel id="panel1">
<script>
var SelectConId1='';

function transfervalue(na)
{

if(na=='pageid:formid:j_id3:j_id4:j_id5'||na==null)
{
alert('Please select a value');
}
else
{
var setname = "{!$CurrentPage.parameters.nameid}";

window.parent.opener.document.getElementById("{!$CurrentPage.parameters.nameid}").value = na;
window.parent.close();

}
return false;
}
function checkone(cb)
{
var countchbox=0;
if(cb.checked)
{

if((SelectConId1.length)<=1)
{
SelectConId1=cb.name;
}
else
{
SelectConId1=SelectConId1+','+cb.name;
}
}
else if((cb.checked)!=true)
{
if((SelectConId1.length)==1)
{
alert('Hello'+cb.checked);
SelectConId1=cb.name;
}
else
{
var allids=SelectConId1.split(',');
var idarray='';
for(var i=0;i<allids.length;i++)
{
if(allids[i]!=cb.name && allids[i]!='')
{
idarray=idarray+','+allids[i];
}
}

}
}

return SelectConId1;
}
function deselectAll()
{
SelectConId1='';
return false;
}
</script>

<apex:image url="{!URLFOR($Resource.lookupwindowHeader, 'image/lookupwindowhr.gif')}" />
<apex:pageBlock >

<apex:pageBlockButtons location="both" >
<apex:commandButton value="Insert Selected" onclick="return transfervalue(checkone(this));"/>
</apex:pageBlockButtons>
<apex:outputLink onclick="deselectAll();" >Deselect All</apex:outputLink>
<apex:pageBlockTable value="{!lookupvalue}" var="cc" id="pbtable">
<apex:column style="width:3%;">
<apex:facet name="header">
</apex:facet>
<input type="checkbox" name="{!cc}" onclick= "checkone(this) " id="checkedone" />
</apex:column>
<div id="{!cc}">
<apex:column >
<apex:facet name="header" >{!$CurrentPage.parameters.Sobject}&nbsp;Name</apex:facet>
{!cc}
</apex:column>
</div>
</apex:pageBlockTable>

</apex:pageBlock>

<!--/div-->
<div class="pbFooter secondaryPalette">
<div class="bg"></div>
</div>


<!--<center> <apex:commandButton value="Insert Selected" id="theButton" onclick="return transfervalue(checkone(this));"/></center>-->


<div id="norecord" style="display:none;">
No Record Found
</div>
</apex:outputPanel>
</apex:form>
</apex:page>


public class DescribeSobjectTest
{
public list<string> lookupvalue{get;set;}
public list<sObject> datalist{get;set;}
Map<String, Schema.SObjectField> M;
public List<SObjectField> ObjField{get;set;}
public List<SelectOption> fieldsvalues{get;set;}


public DescribeSobjectTest()
{
lookupvalue = new list<string>();

M = new Map<String, Schema.SObjectField>();
ObjField = new List<SObjectField>();
fieldsvalues = new List<SelectOption>();

string queryObjectstr = ((ApexPages.currentPage().getParameters().get('Sobject') == null || ApexPages.currentPage().getParameters().get('Sobject') == '')?null:ApexPages.currentPage().getParameters().get('Sobject'));

if(queryObjectstr == 'Contact')
{
M = Schema.SObjectType.Contact.fields.getMap();
}
if(queryObjectstr == 'Account')
{
M = Schema.SObjectType.Account.fields.getMap();
}
if(queryObjectstr == 'Contact')
{
M = Schema.SObjectType.Contact.fields.getMap();
}
Schema.SObjectField f21 = M.get('Name');

ObjField = M.values();
for(integer j = 0 ; j < ObjField.size() ; j++)
{
String str = String.Valueof(ObjField[j]);
fieldsvalues.add(new SelectOption(str,str));
}

string picklistvalue2 = ((ApexPages.currentPage().getParameters().get('pickval') == null || ApexPages.currentPage().getParameters().get('pickval') == '' || ApexPages.currentPage().getParameters().get('pickval') == 'undefine')?null:ApexPages.currentPage().getParameters().get('pickval'));
if(picklistvalue2 != null)
{
Schema.SObjectField f = M.get(picklistvalue2);
system.debug('*******' + f.getDescribe().getType());
if(String.Valueof(f.getDescribe().getType()) == 'BOOLEAN' || String.Valueof(f.getDescribe().getType()) == 'PICKLIST' || String.Valueof(f.getDescribe().getType()) == 'REFERENCE' || String.Valueof(f.getDescribe().getType()) == 'MULTIPICKLIST')
{

if (String.Valueof(f.getDescribe().getType()) == 'PICKLIST')
{
lookupvalue = new List<String>();
List<Schema.PicklistEntry> ple = f.getDescribe().getPicklistValues();
for(Schema.PicklistEntry p : ple)
lookupvalue.add(p.value);
}
if (String.Valueof(f.getDescribe().getType()) == 'MULTIPICKLIST')
{
lookupvalue = new List<String>();
List<Schema.PicklistEntry> ple = f.getDescribe().getPicklistValues();
for(Schema.PicklistEntry p : ple)
lookupvalue.add(p.getValue());
}
else if(String.Valueof(f.getDescribe().getType()) == 'BOOLEAN')
{
lookupvalue = new List<String>();
lookupvalue.add('True');
lookupvalue.add('False');
}
else if(String.Valueof(f.getDescribe().getType()) == 'REFERENCE')
{
lookupvalue = new List<String>();
list<Schema.SObjectType> queryObject = f.getDescribe().getReferenceTo();
list<sObject> datalist = new list<sObject>();
if(picklistvalue2 != 'RecordTypeId')
datalist = Database.query('select id,name from '+queryObject[0]+' limit 1000');
else
datalist = Database.query('select id,name from RecordType where SobjectType=\''+queryObjectstr+'\' limit 1000');

if(datalist.size()>0)
{
for (sObject d : datalist)
{
lookupvalue.add((String)d.get('Name'));
}
}
}
else
{
}
}
}
}
public void addThis()
{
}

}

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

vinothvinoth

Hi thanks for your reply. Can u tell me what all the parameter I need to pass through URL? What value i need to pass for the "pickval" parameter?

satyamsatyam

Hi Ankit,

 

 

Actually me also having same type of requirement.

 

I am not getting this code properly can u please send detail code with some inline description so that i can understand and implement my requirement.

 

Thanks in Advance:)))))