+ Start a Discussion
Cristian TrifCristian Trif 

fetch the value of User Language and set it as default


I have the following code:

//Get Language values
        Schema.DescribeSObjectResult contactObjResult = Contact.sObjectType.getDescribe();
        Schema.DescribeFieldResult languageFieldResult = Schema.SObjectType.Contact.fields.Language__c;
        languageFieldResult = languageFieldResult.getSObjectField().getDescribe();

        List <Schema.PicklistEntry> lstLanguageValues = languageFieldResult.getPickListValues();
        this.Languages = new List<SelectOption>();
        for (Schema.PicklistEntry entry : lstLanguageValues) {
            this.Languages.add(new SelectOption(entry.getValue(), entry.getLabel()));

This code only is fetching all the values from the Language__c field. The requirement is to fetch the value of the User Language and set it as default, so for example if my User language is 'English' in my list should be the value, by default english. The  thing is we have translates and I'm not sure how to fetch the value, translate and then make it as default.
Raj VakatiRaj Vakati
Use this code
String currentuserId = UserInfo.getUserId();
        User currentUserInfo = [select LanguageLocaleKey from User where Id = :UserInfo.getUserId()];
        currentUserInfo.LanguageLocaleKey = 'en_US';
        update currentUserInfo;

Maharajan CMaharajan C
HI Cristian,

I hope you are trying the default value for SelectOptions in Visualforce Page:

If yes then in your Controller Constructor Add the Below Lines:

Public Class YourClass {
    public string defautLang {get; set;}
    public YourClass() {                // Constructor
        String userLang = UserInfo.getLanguage();
        for(PicklistEntry value: User.LanguageLocalekey.getDescribe().getPicklistValues()) {
            if(value.getValue() == userLang) {
                defautLang = value.getLabel();
        System.debug('@@@ language ==> ' + defautLang);


Then in the VF Add the below things: 

<apex:selectList value=”{!defautLang}” size=”1″ multiselect=”false”>
<apex:selectOptions value=”{!Languages}“>  // Ypu

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!