function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DipakDipak 

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

Best Answer chosen by Admin (Salesforce Developers) 
cloudmaniacloudmania

 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

cloudmaniacloudmania

 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.
}

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

Hi cloudmania,

 

How to put these labels on visualforce page in a Table?

 

Thanks,

Devendra

cloudmaniacloudmania

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>

DipakDipak

Hi cloudmania,

 

Thanks for your quick reply. My problem is solved and it is working .Thanks.

 

 

 

:smileyhappy:

 

 

 Dipak

Jeff BloomerJeff Bloomer

Take this one further.  How would you return the Id's for the Object's fields instead of the label names?

keshin okawakeshin okawa
I need help...
How do I sort the object names(a to z) before passing them to the visualforce page?
Mikal HansonMikal Hanson

Keshin,

Not really for you, but for those who come after, at the end of your query add "ORDER BY 'my_field_name' ASC".

ernesto truyen 11ernesto truyen 11
thank you very much, this has been useful