10

Im working in a site, And I need to present many fields to be filled and the page, so accoding to a selecction before, I have all the field but I have the Api Name, and the object name, but, With this api object field name, how can I get the label????

Example. I have "Student__c", I need to present "Student" to the user.

Thanks a lot for ur help!


Ok, thanks for ur answuers but I think I did not explain well this situation.

I have a list of string, where I have the information but in Api's name. I present this fields in a table, so the user is filling the fields but with an inputText and not with an inputfield!.. So, in this moment, I dont know what is the label of each field.

My question basically, if by means of a query I could know the label of a field, if I know the api field name and the object name.. My question is if there is a way to consult this info!...

I know if I have the object is enough using an inputfield, But I cant do that. Neither set the label with HTML directly 'cause this is something dynamic.

Anyone knows how can I know, or consult, I guess by means of a query, how to get this label names???

1
  • Are you creating a Visualforce page? Dec 20, 2013 at 16:23

6 Answers 6

8

Not sure if you can do it in pure visualforce. With Apex controller you'll be able to do something like this:

String objectName = 'Contact';
String fieldName = 'FirstName';

List<Schema.DescribeSObjectResult> describeSobjectsResult = Schema.describeSObjects(new List<String>{objectName}); // this can accept list of strings, we describe only one object here
System.debug(describeSobjectsResult);
String objectLabel = describeSobjectsResult[0].getLabel();
Map<String, Schema.SObjectField> allFields = describeSobjectsResult[0].fields.getMap();
System.debug(allFields);
String fieldLabel = allFields.get(fieldName).getDescribe().getLabel();

System.debug(objectName + '.' + fieldName + '  => ' + objectLabel + ', ' + fieldLabel);

Totally dynamic retrieval of object label & field label ;)

See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm for more goodies. Maybe it'd be possible in pure Visualforce with the ['string here'] syntax...

Also... exactly how dynamic you'll have to be? Check out the fieldsets in VF developer guide, might simplify your page greatly...

3
  • Not sure why this got downvoted, but this is the best way to do this. Anything else relies on hardcoding the label in code instead of referencing the metadata. Dec 20, 2013 at 21:49
  • Hi @MichaelWelburn, thx for the kind words :) No idea really. We don't know how dynamic the solution has to be. If the fields point to some fields on some sObject(s) - I see only my way. If there is a concrete Account etc. somewhere in the controller - I like sfdcfox's way as it feels cleaner and closer to fieldsets solution.
    – eyescream
    Dec 21, 2013 at 12:29
  • This is the real answer I was looking for!... So Thank you. I figure it out from another website, but its your same answer...
    – user4882
    Dec 23, 2013 at 21:20
15

Use $ObjectType global variable, and it could print the Student__c field label like this(please change the object name as needed)

<apex:outputLabel value="{!$ObjectType.CustomObject__c.fields.Student__c.Label}" />
4

You can generate labels dynamically:

Controller

public with sharing class dyno {
    public Account account { get; set; }
    public String[] fields { get; set; }
    { account = new Account();
      fields = new String[] { 'Name','Industry' };
      }
}

Page

<apex:page controller="dyno">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!fields}" var="field">
                <apex:column value="{!$ObjectType.Account.fields[field].Label}"/>
                <apex:column >
                    <apex:inputText value="{!account[field]}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
4

another simple way to display field label from apex, is like this (if the object and field is not to be dynamic) :

 Schema.SObjectType.Contact.fields.Student__c.getLabel()

If it is to be displayed on vf, then you can use the following code :

<apex:outputText value="{!$ObjectType.Contact.fields.Student__c.Label}"/>
1
  • The nice thing about using the Schema approach is that it checks that the field exists at compile time. If you use a Map you could get a runtime exception.
    – dana
    Jul 18, 2017 at 21:42
3

An example of using the label attribute in a simple Visualforce Controller:

<apex:page standardController="My_Custom_Object__c" >
    <apex:form>
        <apex:pageBlock title="My Section Title">
            <apex:pageBlockSection columns="2" title="Section Header">
                <apex:inputField value="{!My_Custom_Object__c.Student__c}" label="Student"/>
            <apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Another more general example:

<apex:page standardController="My_Custom_Object__c" >
    <style>
        .myLabel{
            margin-right:20px;
        }
    </style> 
    <apex:form>
        <apex:outputText value="Student" styleClass="myLabel"/><apex:inputField value="{!My_Custom_Object__c.Student__c}"/>
    </apex:form>
</apex:page>
1

If all you have is a string, here's another simple solution:

String objectName = 'CustomObject__c';

Schema.SObjectType sObjectType = Schema.getGlobalDescribe().get(objectName);
Schema.DescribeSObjectResult describeSobjectsResult = sObjectType.getDescribe();
String objectLabel = describeSobjectsResult.getLabel();

System.debug(objectLabel);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .