Is there anyway to get the name/label of a field without doing a field describe?

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?

Answer

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()){
            describes++;
            System.debug('Ran describe ob '+fld.getDescribe().getName());
        }
    }
}
System.debug(LoggingLevel.Error, 'Ran a total of '+describes+'field describes');

Attribution
Source : Link , Question Author : Kiet , Answer Author : ca_peterson

Leave a Comment