Custom Multiselect Look-up

Hii All,

There is always an requirement and also an idea that is being proposed of having Multiselect Lookup Relationship betweenthe 2 objects that might be between either an Standard Objects or can be between custom objects but till now there has nothingavailable of such type of nothing in Salesforce.

So here is it. As you can achieve everything what you desire of through coding, so we would create multiselect lookupalso with help of Apex and vf pages thorugh which we can add as many records in one time thorugh custom lookups.

So to start with the functionality first you will haver to create a Vf page with its custom controller and also will have a standard controller of the desired object that you might take according to your requirement so that you can make it an inlinevf page on the detail page of your records.

In my code since I have worked upon relation between Account and Contact, so I have created a Vf page with standard controllerof Account that adds up multiple contacts from the detail page of Account record and also an custom controller namedcustomMutliSelectLookUpCntrl.

Below is my code for the vf page:-

<apex:page standardController=”Account” extensions=”customMutliSelectLookUpCntrl”><style type=”text/css”>.customPopup{ background-color: white; border-style: solid; border-width: 2px; left:20%; padding:10px; position: absolute; z-index: 9999; width: 500px; top:20%;} .disabledTextBox{ background-color: white; border: 1px solid; color: black; cursor: default; width: 90px; display: table; padding: 2px 1px; text-align: right;}.closeButton{ float: right;}</style> <apex:form > <apex:pageBlock id=”counter”> <apex:inputtextarea value=”{!lookUp}” label=”Contact”/> <apex:commandButton value=”Add” reRender=”out” action=”{!add}”/> <apex:outputPanel id=”out”> <apex:outputPanel styleClass=”customPopup” rendered=”{!bool}”> <apex:commandButton value=”X” title=”Close the popup” action=”{!closePopup}” styleClass=”closeButton” rerender=”out”/> <apex:pageBlockTable value=”{!show}” var=”e” title=”show”> <apex:column > <apex:inputCheckbox value=”{!e.check}”/> <apex:actionSupport event=”onclick” action=”{!inIt}”/> </apex:column> <apex:column > <apex:commandLink value=”{!e.con.Name}”/> </apex:column> </apex:pageBlockTable> </apex:outputPanel> </apex:outputPanel> <apex:commandButton value=”Save” action=”{!mySave}”/> </apex:pageBlock> </apex:form></apex:page>

As you can see that there is also an custom controller associated with it which controls the functionality of the above page.So once you have created the above page just copy and paste the below apex class code :-

public with sharing class customMutliSelectLookUpCntrl{

public string lookUp{get;set;} public list<conContact> contactList{get;set;} public boolean allbool{get;set;} public string inputValue{get;set;} public boolean bool{get;set;} public set<id> contactids{get;set;} ApexPages.StandardController controller; public customMutliSelectLookUpCntrl(ApexPages.StandardController con){ controller = con; contactList = new list<conContact>(); bool = false; contactids = new Set<Id>(); } public class conContact{ public contact con{get;set;} public boolean check{get;set;} public conContact(contact c, boolean boo){ con = c; check = boo; } } public void inIt(){ list<Contact> selectedContact = new list<Contact>(); lookUp = ”; for(conContact conObj : contactList){ if(conObj.check != false){ system.debug(‘conObj.con’+conObj.con); selectedContact.add(conObj.con); lookUp += + ‘ ‘; system.debug(‘lookUp::’+lookUp); contactids.add(; } } bool = true; } public list<conContact> getShow(){ for(Contact coObj:[select id,name from Contact]){ contactList.add(new conContact(coObj,allbool)); } return contactList; } public PageReference mySave(){

list<Contact> updateSelectedContact = new list<Contact>(); id accId = ApexPages.CurrentPage().getparameters().get(‘id’); for(Contact co:[select id,name,accountid from Contact where id =: contactids]){ co.accountid = accId; updateSelectedContact.add(co); } update updateSelectedContact; return null; } public void closePopup() { bool = false; } public void add(){ bool = true; }}

As you can see that I have worked upon wrapper class just to select the different records that you want to add to relatedrecords in one time which takes the value back to the controller and saves it back to the related child object.

After the class is being save go to the layout page of the Account object and this page Custom Lookup as an inline VF pageto the objects detail page layout.

Save the layout after adding the VF page and start using the multiselect lookup functionality.