1

I'm trying to use Salesforce describe methods to get an object dynamically, and they get all the labels to that object.

I am trying to use the following code, but the map being returned doesn't include formula fields. Any idea on how I can accomplish this?

Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String,Schema.SObjectField> fieldMap = gd.get( 'Custom_Object__c' ).getDescribe().fields.getMap();

2 Answers 2

1

What about this :

 Map<String, Schema.SObjectField> myFields = Schema.getGlobalDescribe().get( 'MyObject__c' ).getDescribe().fields.getMap();
for (String apiName : myFields.keySet())
{
    Qutils.debug(apiName + '==>'+myFields.get(apiName).getDescribe().getLabel());
}
4
  • how is this dynamic ? Commented Feb 19, 2014 at 23:23
  • updated my answer
    – brovasi
    Commented Feb 20, 2014 at 0:12
  • This works to get the Formula Fields when I know the Object ahead of time, but is there a way to get the Object dynamically also if I don't know it ahead of time?
    – Minutiae
    Commented Feb 25, 2014 at 17:00
  • it's almost the same. I updated my answer
    – brovasi
    Commented Feb 25, 2014 at 22:07
1

If your goal is to show the label via a visualforce page so you don't have static values per each API field, use the following example within the visualforce page to correctly display the label even if it changes on the administrative side:

{!$ObjectType.Campaign.fields.Name.label}
{!$ObjectType.Campaign.fields.CustomFieldAPIName__c.label}

Bonus:

printFields('Account');
public void printFields(String sObjectName){
    SObjectType sot = Schema.getGlobalDescribe().get(sObjectName);
    Map<String,Schema.Sobjectfield> fieldInfo = sot.getDescribe().fields.getMap();
    for(String s :fieldInfo.keySet()){
         system.debug('APIName: ' + s + '\nAPIFieldLabel: ' + fieldInfo.get(s).getDescribe().getLabel()); 
    }
}

You must log in to answer this question.

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