3

How can we set a default value for selectoption.

I have a custom setting which list several languages and on VF we have to display a all values from custom setting making a default value based on user's language.

e.g.: if Users default language is French, select option on Vf should display FR.


  • e.g.: if Users default language is French, select option on Vf should display FR. <apex:selectList label="Language" id="language" value="Language" size="1" style="width:100px;" rendered="{!!active}" required="true" multiselect="false" > <apex:selectOptions value="{!usrRec.Language2}" /> </apex:selectList> – Rahul Aug 20 '15 at 5:52
4

You just get userinfo and put it inside selectList variable.

An example would look like this

//controller

Public List<SelectOption> langs {get; set;}
Public String userlang {get; set;}
public myController(){
    langs = new List<SelectOption>();
    langs.add(new SelectOption('en', 'EN'));
    langs.add(new SelectOption('fr', 'FR'));
    langs.add(new SelectOption('tr', 'TR'));
    userlang = UserInfo.getLocale().substringBefore('_'); //this will return user's langauge.

}

//vf page

    <apex:selectList value="{!userlang}" size="1">
        <apex:selectOptions value="{!langs}"/>
    </apex:selectList>
  • That helped a lot.. thanks for your valuable suggestion. – Rahul Aug 20 '15 at 9:13

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.