Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is there a way to get the label of a field without doing a loop on all object fields like this :

Map<String, SobjectField> fieldMap = Account.getsObjectType().getDescribe().Fields.getMap(); 
for(String f :  fieldMap.keySet())
System.debug(fieldMap.get(f).getDescribe().getLabel());

Something like this :

String label = Account.getField('myFieldName').getLabel();
share|improve this question
    
Do you intend to you use it in a visualforce page? –  melmoussaoui Apr 22 at 9:35
    
Yes that's the goal. –  SF_user Apr 22 at 9:36

3 Answers 3

up vote 2 down vote accepted

If you want to do this in VisualForce you can use the following pattern:

{!$ObjectType.Account.Fields.['MyField__c'].Label} 

Account can also be replaced by ['Account'] in the above line

If you have a List of field names that you need to iterate over then you can use the apex:repeat tag. See the example below

<apex:repeat value="{! fieldNames}" var="fieldName"> 
    <apex:outputText value="{!$ObjectType.Account.Fields.[fieldName].Label}">
</apex:repeat>
share|improve this answer
    
That's what I need, thanks ! –  SF_user Apr 22 at 12:01

In Apex code, if you want the label of a field that you know about at compile time you can directly use the SObjectField reference (Account.BillingCity in this case but can be your custom fields too) and get the describe information from that:

String label = Account.BillingCity.getDescribe().getLabel();

But if you only know the field name at runtime, there is a map available on the fields collection that you can use without a loop. For example:

String label = Account.SObjectType.getDescribe().fields.getMap().get('BillingCity').getDescribe().getLabel();

PS Just noticed the comment that you want to do this in Visualforce not Apex. So you should follow BarCotter's approach.

share|improve this answer
    
This can also be done directly in VisualForce using this pattern: {!$ObjectType.Account.Fields.['BillingCity'].Label} –  BarCotter Apr 22 at 10:04
    
@BarCotter : I don't know the name of the field. It's on a loop so can I do something like : {!$ObjectType.Account.Fields.[myLoop[i]].Label} where myLoop[i] represent an Account field ? –  SF_user Apr 22 at 10:11
    
I have added an answer with the details –  BarCotter Apr 22 at 10:34

then use this :

{!$ObjectType.Account.Fields.Name.Label}

Where Name is you field

share|improve this answer
    
I see this but the problem is that I have a random Account fields list. I have to loop on all fields and get the field label. I already ask about it (in Javascript) : salesforce.stackexchange.com/questions/33124/… –  SF_user Apr 22 at 9:39
    
Ah, then in this case I don't think that you have another choice than what you already did in you question. –  melmoussaoui Apr 22 at 9:42
    
You speak about the loop ? –  SF_user Apr 22 at 9:43
    
Yes. I don't know about another way to get dynamically fields and labels for an SObject, and the simplest way as @Keith C told you. if you already knew the field name. –  melmoussaoui Apr 22 at 9:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.