multi records selection from lookup in salesforce using apex
Create object SELECT Name,id FROM Affiliation_Details__c WHERE name Like '+byName +'
Step: 2
Create page where look up
<apex:page standardController="User_Affiliation_Details__c" id="pageId" >
<apex:form id="mainFormId">
<br/><br/><br/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/JavaScript" />
<apex:pageBlock title="New Client" id="pgBId" >
<apex:pageMessages />
<table>
<tr>
<td>Affiliations Name</td>
<td>
<apex:inputHidden id="phoneNumberFieldId" value="{!selectedaffID}"/>
<apex:inputTextarea disabled="true" id="phoneNameFieldId" style="margin: 0px; width: 161px; height: 56px;" value="{!affiliation}" />
<a href="#" id="acc3_lkwgt" onclick="openLookupPopup();" tabindex="2" title=" Lookup (New Window)" style="text-decoration: none;">
<img src="/s.gif" alt="Parent Account Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Parent Account Lookup (New Window)"/>
</a>
</td>
</tr>
<tr>
<td></td>
<td>
<apex:commandButton value="Create Client" action="{!save}" />
</td>
</tr>
</table>
</apex:pageBlock>
<script>
var $ = jQuery.noConflict();
$(document).ready(function(){
$("#toggleId").click(function(){
$("#detailTblId").toggle();
if($("#detailTblId").css('display')=='none'){
$('#linkTextId').html('Click here to show the Contact information in the Tabular form.');
}else{
$('#linkTextId').html('Click here to Hide.');
}
});
});
var newWin=null;
function openLookupPopup(){
var objectName = $('select[id$=selectedObjectId]').val();
var fieldName = $('select[id$=selectfield]').val();
var SelectId= $('[id$=phoneNumberFieldId]').val();
try{
var url="/AffliationManager/apex/CustomLookup?id="+SelectId+"&core.apexpages.devmode.url=1" ;
newWin=window.open(url, 'Popup','height=400,width=650,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
if (window.focus){
newWin.focus();
}
// }
}catch(e){
alert('Server Error: '+e);
}
return false;
}
function closeLookupPopup(){
if (null!=newWin)
{
newWin.close();
}
}
</script>
</apex:form>
</apex:page>
Step3:
Create page name CustomLookup
<apex:page Controller="CustomLookup_ctrl" tabStyle="User" sidebar="false" id="pageId" showHeader="false">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/JavaScript" />
<apex:actionStatus id="ajaxStatus">
<apex:facet name="start">
<div style="position: fixed; top: 0; left: 0; right: 0; bottom: 0; opacity: 0.25; z-index: 190000; background-color:#fff;">
</div>
<div style="position: fixed; left: 0; top: 0; bottom: 0; right: 0; z-index: 190000; margin: 18% 50%">
<div style="display: inline-block; padding: 2px; background-color: #fff; width: 125px;">
<img src="/img/loading.gif" style="float: left; margin: 8px;" />
<span style="display: inline-block; padding: 10px 0px;">Please Wait...</span>
</div>
</div>
</apex:facet>
</apex:actionStatus> <style>
.outerdiv {
background-repeat: repeat-x;
background-position: left top;
background: white url(/img/alohaSkin/lookup_bg.png) repeat-x;
padding-left: 10px;
height: 100%;
}
.innerdiv {
background-image: url(/img/sprites/master.png);
background-position: 0px -1202px;
width: 32px;
height: 32px;
}
.requiredLoaderBG{
background-color: rgb(71,71,71);
opacity:0.7;
filter:alpha(opacity=70);
background-position: center center;
background-repeat: no-repeat;
height: 100%;
left: 0;
position: fixed;
text-align: center;
top: 0;
width: 100%;
z-index: 1;
}
</style>
<apex:form >
<div class="outerdiv">
<input type="button" value="Close Window" style="float:right;" onclick="onClose();"/>
<div class="innerdiv"></div>
<div style="float: left; width: 50px; margin-top: -20px; margin-left: 40px">
<h1>Lookup</h1>
</div>
<apex:pageMessages id="msgId"/>
<div class="pbBody">
<table class="list" border="0" id="tblData" cellspacing="0" cellpadding="0" style="border: #DDD 1px solid;width:40%;">
<tr class="headerRow" >
<th>
<b>Name</b>
</th>
</tr>
<tr>
<td>
<apex:inputText value="{!searchByName}" /> <apex:commandButton onclick="Search_name();" value="Go!" oncomplete="return false;" />
<apex:actionFunction name="onSearch" action="{!onSearch}" />
</td>
</tr>
</table>
</div>
<div class="pbBody"> <br/>
<h2 style="padding-bottom: 10px; padding-top:20px;">
Select the picklist values to add below. <br/><br/>
</h2>
</div>
<apex:pageblock >
<apex:outputPanel id="tblId">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!ListDetails}" var="e">
<apex:column width="2%">
<apex:inputCheckbox id="checkBoxId" styleClass="case" value="{!e.isChecked}" >
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Affiliation Name" >{!e.Name}
<apex:inputHidden value="{!e.Name}" id="hdnName" />
<apex:inputHidden value="{!e.Id}" id="hdnId" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageblock>
<apex:inputHidden value="{!SelectId}" id="SelectId" />
</div>
<script>
//function fillIn(id){
$(".case").click(function(){
var ids='';
var name='';
$('input[type=checkbox]').each(function () {
if (this.checked) {
ids+= $(this).closest('tr').find('input[id$=hdnId]').val()+',';
name+= $(this).closest('tr').find('input[id$=hdnName]').val()+',';
}
});
ids= ids.substring(0, ids.length-1)
name= name.substring(0, name.length-1)
var winMain1 = window.opener;
if (null == winMain1){
winMain1=window.parent.opener;
}
var elem = winMain1.document.getElementById('pageId:mainFormId:pgBId:phoneNumberFieldId');
var elemName = winMain1.document.getElementById('pageId:mainFormId:pgBId:phoneNameFieldId');
elem.value = ids;//document.getElementById('mainPageId:mainFormId:csvPhoneNumberId').value;
elemName.value =name;//document.getElementById('mainPageId:mainFormId:csvPhoneNameId').value;
});
// }
function onClose(){
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}
winMain.closeLookupPopup();
}
//To select All
function selectAll(src){
if(src.checked){
$('.case').attr('checked', true);
}else{
$('.case').removeAttr("checked");
}
fillIn();
}
function Search_name()
{
var winMain1 = window.opener;
if (null == winMain1){
winMain1=window.parent.opener;
}
var elem = winMain1.document.getElementById('pageId:mainFormId:pgBId:phoneNumberFieldId');
//var elemName = winMain1.document.getElementById('pageId:mainFormId:pgBId:phoneNameFieldId');
$('[id$=SelectId ]').val(elem.value);
// alert($('[id$=SelectId ]').val());
onSearch();
}
$(document).ready(function(){
// Search_name();
});
</script>
</apex:form>
</apex:page>
Step 4:
Create controller CustomLookup_ctrl
public with sharing class CustomLookup_ctrl {
public PageReference onCheck() {
return null;
}
public PageReference onSelectAll() {
return null;
}
public String isAllSelectCheckBox { get; set; }
public String listOfLookupsElement { get; set; }
public String searchByName { get; set; }
public String SelectId{ get; set; }
public CustomLookup_ctrl(){
if(apexpages.currentpage().getparameters().get('id')!=null)
SelectId =apexpages.currentpage().getparameters().get('id') ;
else
SelectId ='';
searchByName='';
onSearch();
}
public void onSearch(){
ListDetails=new list<ListDetail>();
//SelectId =',a3E1D0000008p0NUAQ,a3E1D0000008onUUAQ,a3E1D0000008onZUAQ';
string id_new='';
if(SelectId!='' && SelectId!=null)
{
try{
list<string> ll= SelectId.split(',');
id_new='\''+String.join( ll, '\',\'')+'\'';
String query_aff ='SELECT Name,id FROM Affiliation_Details__c WHERE ';
query_aff+=' id in('+id_new+')';
//searchByName =query_aff;
list<Affiliation_Details__c > ss = Database.query(query_aff);
for(Affiliation_Details__c sObj:ss){
ListDetail obj=new ListDetail();
obj.isChecked=false;
obj.name=sObj.name;
obj.id=sObj.id;
obj.isChecked =true;
ListDetails.add(obj);
}
}catch(exception e){}
}
// searchByName='ssssadas';
String byName = '\''+String.escapeSingleQuotes(searchByName) + '%\'';
//try{
String query ='SELECT Name,id FROM Affiliation_Details__c WHERE name Like '+byName +' ';
if(id_new!='')
{
query +=' and id not in('+id_new+')';
}
query +=' limit 20 ';
list<Affiliation_Details__c > s = Database.query(query);
for(Affiliation_Details__c sObj:s){
ListDetail obj=new ListDetail();
obj.isChecked=false;
obj.name=sObj.name;
obj.id=sObj.id;
ListDetails.add(obj);
}
// }catch(Exception ex){
// apexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Illegal Search parameter!'));
// }
}
public list<ListDetail> ListDetails{ get; set; }
public class ListDetail{
public boolean isChecked {get;set;}
public string Name {get;set;}
public string Id{get;set;}
}
}

Comments
Post a Comment