Storing values in Multi-Select picklist using Apex in Salesfoce

Sample Code:

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;
    }
}



Note:
Here Member__c is my Custom Object and Interests__c is a multi-picklist field.

Output:



Cheers!!!

8 comments:

  1. Compile Error: Invalid type: Member__c at line 18 column 27

    ReplyDelete
  2. Hi,
    Here Member__c is my Custom Object and Interests__c is a multi-picklist field.

    Cheers!!!

    ReplyDelete
  3. Error: Storing_values_form_Multi_Selectpicklist Compile Error: Loop must iterate over a collection type: I am getting this error, in for loop it is not taking sting str, pls help me on this

    ReplyDelete
  4. Worked very well .. Thanks a lot :)

    ReplyDelete
  5. how can we delete the saved multipicklist values in salesforce using apex?

    ReplyDelete
  6. Alternatively, just use the String.join() method. Mem.Interests__c = String.join(SelectedInterests, ';')

    Quick, easy to read, and saves about ten lines.
    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

    ReplyDelete