You need to sign in to do that
Don't have an account?

How to get the label Names for Object's field
Hi,
I want to fetch the list of label names for all the fields of an Object....I got some code to get field labels of an Object
i.e. <apex:column title=”{!$ObjectType.OpportunityLineItem.fields.Quantity.label}” headerValue=”{!$ObjectType.OpportunityLineItem.fields.Quantity.label}” width=”50px”>
But I want to get the list of all field labels by Apex..
I got the API name of all fields like this
String type='Account';
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()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
}
But I need to get the label name..
Please help
String type='Account';
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()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
}
All Answers
String type='Account';
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()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
}
Hi cloudmania,
How to put these labels on visualforce page in a Table?
Thanks,
Devendra
It ıs simple..
public List<String> AllLabels{get;set;}
//Do not forget to get instance in constructor for this list
String type='Account';
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()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
AllLabels.add(fieldMap.get(fieldName).getDescribe().getLabel());//It provides to get the object fields label.
}
<apex:pageBlockTable var="lab" value="{!AllLabels}">
<apex:column headerValue="Labels">
{!lab}
</apex:column>
</apex:pageBlockTable>
Hi cloudmania,
Thanks for your quick reply. My problem is solved and it is working .Thanks.
:smileyhappy:
Dipak
Take this one further. How would you return the Id's for the Object's fields instead of the label names?
How do I sort the object names(a to z) before passing them to the visualforce page?
Keshin,
Not really for you, but for those who come after, at the end of your query add "ORDER BY 'my_field_name' ASC".