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();
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());
}
  • how is this dynamic ? – Samuel De Rycke Feb 19 '14 at 23:23
  • updated my answer – Cloud Ninja Feb 20 '14 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 Feb 25 '14 at 17:00
  • it's almost the same. I updated my answer – Cloud Ninja Feb 25 '14 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()); 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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