[SalesForce] I have api objectfield name, how can I get the label

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

Best Answer

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

Related Topic