Activity Forums Salesforce® Discussions How to create a multi-select lookup relationship between two custom objects?

  • Prakhar

    Member
    April 30, 2016 at 12:28 pm

    Hello Hazel,

    Below is the code that works for the multiselect lookup between the 2 objects. I have used it for Standard objects Account and Contact, you can modify it as per your use for the 2 custom objects.

    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>
    So once you have created the above page just use 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 += conObj.con.name + ' ';
    system.debug('lookUp::'+lookUp);
    contactids.add(conObj.con.id);
    }
    }
    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;

    }
    }

    Once you have modified the code according to your requirement and the vf page as inline vf page to the layout of the record.

    Thanks.

  • Gayathri Dasari

    Member
    January 23, 2017 at 9:31 am

    This is very useful post. Great.

    But how do I add the selected values of lookup as individual rows in parent page??

    Any help.?

    Thanks in advance..

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos