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
Deepika TestDeepika Test 

Multi Select Picklist Value storing

How to save multi select picklist values present in my VF page in multi select list field of standard object?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Deepika,
  • Storing values in Multi-Select Picklist using Apex in Salesforce.
Visualforce page:

<apex:page controller="Sample">
<apex:form >    
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Member Name:</apex:outputLabel>
                <apex:inputText value="{!Name}"/>                
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Member Interest:</apex:outputLabel>
                <apex:selectList value="{!SelectedInterests}" multiselect="true" size="3">
                    <apex:selectOptions value="{!InterestOptions}"/>
                </apex:selectList>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Save" action="{!sav}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>
 
Apex Controller:

public class Sample 
{    
    public List<String> SelectedInterests {get;set;}
    public String Name {get;set;}
    
    public List<SelectOption> getInterestOptions() {
        List<SelectOption> Options = new List<SelectOption>();
        Options.add(new SelectOption('None','None'));
        Options.add(new SelectOption('Cricket','Cricket'));
        Options.add(new SelectOption('Hockey','Hockey'));
        Options.add(new SelectOption('Swimming','Swimming'));
        Options.add(new SelectOption('Rugby','Rugby'));
        return Options;
    }
    
    public void sav() {
        Member__c Mem = new Member__c();
        Mem.Name = Name;
        String Interests = '';
        Boolean Start = true;
        if(!SelectedInterests.isEmpty()) {           
            for(String Str : SelectedInterests) {
                if(Start) {
                    Interests = Str;
                    Start = false;
                } else {               
                    Interests = Interests + ';' + Str;
                }
            }
        }
        Mem.Interests__c = Interests;
        insert Mem;
    }
}

Please refer the below link for reference. hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar