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;
}
}
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;
}
}
No comments:
Post a Comment