1

I am trying to mark selected values in a MutliSelect list but nothing is selected when the page loads even though I have selections made.

Visualforce page:

<select class=" select" size="6" aria-describedby="" multiple="true" value="{!SelectVals}">
  <apex:repeat value="{!PickListValuesIntoList}" var="string" id="theRepeat">
     <option label="{!string}" />
  </apex:repeat>
</select>

Controller:

public List<String> PickListValuesIntoList;
public List<String> SelectVals;

public List<String> getPickListValuesIntoList(){
  List<String> pickListValuesList= new List<String>();
  Schema.DescribeFieldResult fieldResult = Opportunity.Products_To_Price__c.getDescribe();
  List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
  for( Schema.PicklistEntry pickListVal : ple){
      pickListValuesList.add(pickListVal.getLabel());
  }     
  return pickListValuesList;
}
public List<String> getSelectVals(){
  list<string> tempSelectVals = new list<string>();
  tempselectvals = ThisOpportunity.Products_to_Price__c.split(';');
  return tempselectvals;
}
1

2 Answers 2

0

You'll want to use apex:selectList, not a regular HTML select. The system will automatically select the correct values when you do that.

Here's the modified Visualforce:

<apex:selectList styleClass=" select" size="6" html-aria-describedby="" multiple="true" value="{!SelectVals}">
  <apex:selectOptions value="{!PickListValuesIntoList}" />
</apex:selectList>

You'll need to modify your Apex Code accordingly:

public List<SelectOption> getPickListValuesIntoList(){
  List<SelectOption> pickListValuesList= new List<SelectOption>();
  Schema.DescribeFieldResult fieldResult = Opportunity.Products_To_Price__c.getDescribe();
  List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
  for( Schema.PicklistEntry pickListVal : ple){
      pickListValuesList.add(new SelectOption(pickListVal.getValue(),pickListVal.getLabel()));
  }     
  return pickListValuesList;
}
0

Someone else posted this but deleted it and it actually worked so I'm not sure why it was deleted:

Controller:

public List<SelectOption> PickListValuesIntoList;
public List<String> SelectVals{set;}

public List<SelectOption> getPickListValuesIntoList(){
  List<SelectOption> pickListValuesList= new List<SelectOption>();
  Schema.DescribeFieldResult fieldResult = Opportunity.Products_To_Price__c.getDescribe();
  List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
  for( Schema.PicklistEntry pickListVal : ple){
      pickListValuesList.add(new SelectOption(pickListVal.getValue(),pickListVal.getLabel()));
  }     
  return pickListValuesList;
}
public List<String> getSelectVals(){
  list<string> tempSelectVals = new list<string>();
  tempselectvals = ThisOpportunity.Products_to_Price__c.split(';');
  system.debug(tempselectvals);
  return tempselectvals;
}

Visualforce page:

<apex:form>
 <apex:selectList size="6" html-aria-describedby="" multiselect="true" value="{!SelectVals}">
  <apex:selectOptions value="{!PickListValuesIntoList}" />
 </apex:selectList>
</apex:form>
2
  • I was confused... tagged lightning, but asking about VF.
    – sfdcfox
    Commented Jul 10, 2017 at 17:44
  • my mistake.. I accidentally typed lightning Commented Jul 10, 2017 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .