[SalesForce] How to get the list of all fields(API name,Label,type) of any sObject

I'm trying to build a Dynamic list(table) of records by selecting the Sobject and its fields.So I need to get all the fields with its API name,label,type.But I can't get the label and type.(Tried DescribeSObjectResult Class methods to get label,but it didn't help to get the label dynamically)
I tried it with the following code.

System.debug('Selection-----'+selectedObject);
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
Schema.SObjectType ctype = gd.get(selectedObject); 
Map<String, Schema.SobjectField> fmap = ctype.getDescribe().fields.getMap();   
List<FieldWrap> strList = new List<WrapMap>();
for(String s: fmap.keySet()) {
    WrapMap wmp = new WrapMap();
    wmp.name = s;
    wmp.label = String.valueOf(fmap.get(s));
    strList.add(wmp);
 }

Where FieldWrap is my wrapper class with name and field of String types.
I'm getting the List of API name of fields,but not the Label and type of it.Is there a way to get the label and type of field.If so, please help me.

Best Answer

//It provides to get the object fields label.

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

//It provides to get the object fields data type.

Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();

Your code will look like this..

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
Schema.SObjectType ctype = gd.get(selectedObject); 
Map<String, Schema.SobjectField> fmap = ctype.getDescribe().fields.getMap();   
List<FieldWrap> strList = new List<WrapMap>();
for(String fieldName: fmap.keySet()) {
    WrapMap wmp = new WrapMap();
    wmp.name = fieldName;
    wmp.label = fmap.get(fieldName).getDescribe().getLabel();        
    strList.add(wmp);
 }
Related Topic