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.

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???

share|improve this question
    
Are you creating a Visualforce page? –  amatorVitae Dec 20 '13 at 16:23
add comment

4 Answers 4

up vote 2 down vote accepted

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

share|improve this answer
    
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. –  Michael Welburn Dec 20 '13 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 '13 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 '13 at 21:20
add comment

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}" />
share|improve this answer
add comment

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>
share|improve this answer
add comment

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>
share|improve this answer
add comment

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.