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
SandeepVSandeepV 

Apex SelectList - Default value problem

Hi 

 

Can some one pls. let me know how to set the all valuses in multiselect Pick list as default. here is the code.

 

Controller 

-------------------------

public class sampleCon {
String[] countries = new String[]{};

public PageReference test() {
return null;
}

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Americas','Americas'));
options.add(new SelectOption('AP','AP'));
options.add(new SelectOption('EMEA','EMEA'));
return options;
}

public String[] getCountries() {
return countries;
}

public void setCountries(String[] countries) {
this.countries = countries;
}
}

 

Page 

-------------------------------------------------------

 

<apex:page controller="sampleCon">
<apex:form >
<apex:selectList value="{!countries}" multiselect="true"> Channel :
<apex:selectOptions value="{!items}"/>
</apex:selectList><p/>

<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>

<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

 

Thanks for your help in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
tukmoltukmol

put it in your constructor i.e.

 

public sampleCon() {

  this.countries.add('Americas');

  this.countries.add('AP');

  this.countries.add('EMEA');

}

All Answers

tukmoltukmol

put it in your constructor i.e.

 

public sampleCon() {

  this.countries.add('Americas');

  this.countries.add('AP');

  this.countries.add('EMEA');

}

This was selected as the best answer
SandeepVSandeepV

Thanks tukmol ! 

 

It worked !