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;
}
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>
  • I was confused... tagged lightning, but asking about VF. – sfdcfox Jul 10 '17 at 17:44
  • my mistake.. I accidentally typed lightning – AverageJoeVBA Jul 10 '17 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.