I want to get the name and label of all the fields for multiple sobjects in one transaction and that will easily hit the governor limits. Is there anyway I can get the label/name without using a describe for each field? A mass describe for fields?

  • Can you tell us more about your requirements? Do you need ALL fields on ALL objects at once? Or can the user choose an object then interrogate its fields? – Matt and Neil Apr 10 '14 at 18:45
  • Are we talking about a pure Apex situation, or are you talking about something involving Visualforce? – DavidSchach Apr 10 '14 at 19:20

No, but the limits around field describes are probobly much more generous than you think. You're allowed up to 100 static compile-time field references (e.g. Schema.SobjectType.Contact.fields.name.label).

The limits for dynamic describes are even more generous in most situations, and can be combined with this compile-time format for more flexibility. Quoting a very related answer of mine:

The limits on describes are very confusingly written, you're not actually limited to 100 calls to Schema.SObjectField.getDescribe(), instead you're limited to 100 calls to Schema.DescribeSObjectResult.fields.getMap().

This means that if you have a compile-time reference to the fields instead of using the more dynamic fields.getMap() you won't use any describe limits.

The limit being on fields.getMap() really means that you can't enumerate fields on more than 100 SObject types in a single transaction. If you use some form of caching (I have an open source example) this is a pretty generous limit.

I actually had to defend this assertion on a code review recently, here's some anonymous apex that can experimentally demonstrate this:

List<Schema.SObjectType> objects = new List<Schema.SObjectType>{ Account.SObjectType, Contact.SObjectType, Case.SObjectType, User.SObjectType, Lead.SObjectType};
Integer describes = 0;
while(describes < 500){
    for(Schema.SObjectType objType: objects){
        for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
            System.debug('Ran describe ob '+fld.getDescribe().getName());
System.debug(LoggingLevel.Error, 'Ran a total of '+describes+'field describes');
  • Thanks, I was concerned that each "getDescribe()" would count against the governor limits. – Kiet Apr 10 '14 at 19:21

You can get a Schema.DescribeSObjectResult[] for multiple sObjects with one Schema.describeSObjects() call, but you'll still have to iterate through the results and call Schema.SObjectField.getDescribe() for each field.

Which limit are you concerned about hitting?


You can get all field Names for an object using a single describe. But I think not the label. The DescribeSObjectResult.Fields map keys give field names suitable for use (eg) in Dynamic SOQL.

Here's an example using exactly 1 describe to SELECT all fields on an object by Id:

static public SObject getById(Id id) {
    SObjectType type = id.getSObjectType();
    DescribeSObjectResult description = type.getDescribe();
    String name = description.getName();
    String fields = String.join(new List<String>(description.fields.getMap().keySet()), ',');
    return Database.query('SELECT ' + fields + ' FROM ' + name + ' WHERE Id = :id');

If you want the Label that strikes me as more of a UI concern. And if you break out into Visualforce, you can do as many$ObjectType.CustomObject__c.Fields.CustomField__c.Label as you need. It just isn't so straightforward in Apex.

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.