function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TweetsMcGTweetsMcG 

Populate multi-select picklist from custom object, and add to Lead object

Hi there

 

Am a newb to Apex/VF.  I'm trying to populate a multi-select picklist "Site Categories", from a custom object - Site_Categories__c.

 

I want to add this field to the Lead object.

 

Thanks to some wonderful SF people out there, I've been able to succeed in the first part, however, I'm not sure how to add it to the Lead object, plus be able to edit and save the values chosen to the Lead records.

 

All help appreciated!  Code below:

 

Controller

 

public with sharing class MultiSelectPicklist {

    private final Site_Categories__c scats;

    public MultiSelectPicklist(ApexPages.StandardController controller) {
            
       this.scats= (Site_Categories__c)controller.getSubject();

    }

 
    //public Site_Categories__c scats;
    
    public List<SelectOption> LeftSelectList {get;set;}
    public List<SelectOption> RightSelectList {get;set;}
 
    public List<string> LeftSelectedValues {get;set;}
    public List<string> RightSelectedValues {get;set;}
 
    public MultiSelectPicklist()
    {
        LeftSelectList = new List<SelectOption>();
        RightSelectList = new List<SelectOption>();
        

        for(Site_Categories__c scats : [select id,category__c from site_categories__c order by category__c ASC])
        {
            LeftSelectList.add(new SelectOption(scats.Id, scats.category__c)) ;
        }
        
        RightSelectList.add(new SelectOption('-- None --','-- None --'));
    }
 
    public void MoveRight()
    {
        if (LeftSelectedValues != null && LeftSelectedValues.size() > 0)
        {
            
            //Clear RightSelectedValues
            RightSelectedValues = new List<string>();
            
            //Remove 'None' value from RightSelectList            
            if(RightSelectList != null)
            {
                for(integer i = 0; i < RightSelectList.size(); i++)
                {
                    RightSelectList[i].getValue();
                    RightSelectList.remove(i);
                }
            }
            
            for (integer i =0; i < LeftSelectList.size(); i++)
            {
                for (integer si =0; si < LeftSelectedValues.size(); si++)
                {
                    if (LeftSelectList[i].getValue() == LeftSelectedValues[si])
                    {
                        //Remove the SelectOption from LeftSelectList and add it to RightSelectList

                        RightSelectList.add(LeftSelectList.remove(i));
 
                        //Remove the SelectOption from LeftSelectedValues and add it to RightSelectedValues
                        RightSelectedValues.add(LeftSelectedValues[si]);
 
                    }
                }
 
            }
            //Clear LeftSelectedValues
            LeftSelectedValues = new List<string>();
            LeftSelectList.add(new SelectOption('-- None --','-- None --'));
        }
    }
 
    public void MoveLeft()
    {
        //Remove 'None' value from LeftSelectList
        if(LeftSelectList != null)
            {
                for(integer i = 0; i < LeftSelectList.size(); i++)
                {
                    LeftSelectList[i].getValue();
                    LeftSelectList.remove(i);
                }
            }
            
        if (RightSelectList != null && RightSelectList.size() > 0)
        {
            //Clear LeftSelectedValues
            LeftSelectedValues = new List<string>();

            for (integer i =0; i < RightSelectList.size(); i++)
            {
                for (integer si =0; si < RightSelectedValues.size(); si++)
                {
                    if (RightSelectList[i].getValue() == RightSelectedValues[si])
                    {
                        //Remove the None value from the LeftSelectList
                        //LeftSelectList.remove(i);
                                    
                        //Remove the SelectOption from RightSelectList and add it to LeftSelectList
                        LeftSelectList.add(RightSelectList.remove(i));

 
                        //Remove the SelectOption from RightSelectList and add it to LeftSelectedValues
                        LeftSelectedValues.add(RightSelectedValues[si]);
                    }
                }
 
            }
            //Clear RightSelectedValues
            RightSelectedValues = new List<string>();
            RightSelectList.add(new SelectOption('-- None --','-- None --'));
        }
    }
    

}

 

Page

 

<apex:page controller="MultiSelectPicklist" sidebar="false">
<apex:form >
     <apex:pageBlock mode="edit">
 
        <table cellpadding="3">
            <tr>
                <td colspan="2"><apex:outputLabel style="font-weight: bold;">Site Categories</apex:outputLabel></td>
            </tr>
            <tr>
                <td>
                    <apex:selectList size="5" multiselect="true" value="{!LeftSelectedValues}" id="LeftSelectList">
                        <apex:selectOptions value="{!LeftSelectList}"/>
                    </apex:selectList>
                </td>
                <td style="vertical-align:middle;text-align:center;">
                    <apex:commandButton value=">"  action="{!MoveRight}" reRender="LeftSelectList,RightSelectList"/>
                    <br />
                    <apex:commandButton value="<" action="{!MoveLeft}" reRender="LeftSelectList,RightSelectList"/>
                </td>
                <td>
                    <apex:selectList size="5" multiselect="true" value="{!RightSelectedValues}" id="RightSelectList">
                        <apex:selectOptions value="{!RightSelectList}"/>
                    </apex:selectList>
                </td>
 
            </tr>
        </table>
 
    </apex:pageBlock>
</apex:form>
</apex:page>

 

Please help!

 

Thank you :)

 

R

LakshmanLakshman

Hi,

 

If you want to add this field to sObject Lead, you will have to create a relationship between Lead and your custom object.

For the types of relationship available in salesforce please look into this link.

 

Let me know if have questions.

 

Regards,

Lakshman

 

 

TweetsMcGTweetsMcG

Hi

 

Thanks for your response.

 

I'm only using the custom object to populate the picklist with values.  I'm doing it this way to get around the multi-select picklist size limit.

 

We have over 1,000 values to add.

 

If there is a better way to do this, I'm all ears. :)

 

Thanks!

 

R

LakshmanLakshman

You are going on right path.

For selecting values and then storing them to lead object within which you will have to create a Picklist  field. The limitation for picklist field is:

Up to 1,000 entries
Up to 255 characters per entry


 

Can you tell me what is the average number of characters you have in picklist values, so that I can tell you what would be best for you.

 

Regards,

Lakshman