+ Start a Discussion
s.mafoders.mafoder 

How to get Object Fields labels in a picklist

Hello , 

 

Suppose I have a Salesforce Object with three fields , field1__c,field2__c,field3__c.....

I want to be able in my visualforce page to have a picklist with values field1,field2,Field3..

 

Depending on picklist selecting i update the selected field with an inputText...

 

Many thanks for your Help!!

s.mafoders.mafoder

Pleaaaase Help me !!

souvik9086souvik9086

Use this

 

String type='MyCustObject_c';

List<SelectOption> options = new List<SelectOption>();

        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType leadSchema = schemaMap.get(type);
        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

      

        for (String fieldName: fieldMap.keySet()) { 

        //It provides to get the object fields label.

        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

options.add(fieldLabel);

}

 

In options you will get the required picklist values(field labels). Display that variable in selectlist & selectoption in vf.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

ezdhanhussainezdhanhussain
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();  
    //list method
 public list<selectoption> getFields(){
    String type='Your sobject api name';
List<SelectOption> options = new List<SelectOption>();
     //Creating sObject for dynamic selected object  
            Schema.SObjectType systemObjectType = gd.get(type);  
            //Fetching field results  
            Schema.DescribeSObjectResult r= systemObjectType.getDescribe();  
                  
            Map<String, Schema.SObjectField> M = r.fields.getMap();  
            //Creating picklist of fields  
            for(Schema.SObjectField fieldAPI : M.values())  
            {  
                options.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;  
            }  
            return options;

    }
// vf page
<apex:selectList value="{!Field}" size="1">
  <apex:selectOptions value="{!Fields}">
  </apex:selectOptions>
</apex:selectList>

 Try this...