Are there any governor limits, or considerations that I should be aware of when using Apex Describe methods? So for example, the author of this blog post uses the structure below to get the record type instead of doing a soql query.
http://www.jonathanbroquist.com/retrieving-sobject-recordtypeid-dynamically-with-salesforce/
//Generate a map of tokens for the sObjects in your organization
Map gd = Schema.getGlobalDescribe();
//Retrieve the describe result for the desired object
DescribeSObjectResult result = gd.get('Account').getDescribe();
//Generate a map of tokens for all the Record Types for the desired object
Map recordTypeInfo = result.getRecordTypeInfosByName();
system.debug('RECORD TYPES:' + recordTypeInfo);
//Retrieve the record type id by name
String recordTypeId = recordTypeInfo.get('RECORDTYPE_NAME').getRecordTypeId();
Best Answer
As mentioned, describe limits have been removed entirely. Just for completeness, there are a few remaining limits that are affected by these describe calls, but consumption is negligible. These limits are
Heap Space
andCPU Time
. The numbers below are for my org, so mileage will vary. I doubt the numbers would ever get high enough to be a concern.Methodology
You need separate approaches (though one execution is sufficient) to derive the measures that follow. All of this was run in
Execute Anonymous
with logging levels set toNONE
except forApex
, which was atDEBUG
.Consumption
Schema.getGlobalDescribe()
CPU Time
: ~7msHeap Space
: ~0.3%Schema.SObjectType.getDescribe()
CPU Time
: ~0.002msHeap Space
: ~0.001%Schema.SObjectField.getDescribe()
CPU Time
: ~0.38msHeap Space
: ~0.001%Schema.DescribeSObjectResult.getRecordTypeInfosByName()
CPU Time
: ~0.2msHeap Space
: ~0.0001%Analysis
If you are getting the global describe more than once, cache it. It should be quite simple to cache, and the heap consumption is likely worth the tradeoff even if the number of calls is <50.
For object describes, every 1000 calls (in a single transaction) consume only 2ms (out of 10s), so you don't need to pay much attention unless you are expecting >100K calls. Caching 1000 fields will only consume 1% of your heap space, so that's not a huge concern either. I recommend you use whatever makes your code cleanest.
For field describes, you only get three calls per millisecond, so the calculus is a little different. Now 1000 calls consume an amount of time that is perceptible by humans. However, the cache rate is the same. If you are describing the same field many times, consider caching.
For record type describes, the CPU situation is much the same as with fields. You get five calls per millisecond, so you don't necessarily want to call it thousands of times. But caching it is very inexpensive. You should be able to cache the describes for every single
RecordType
without consuming 1% of your heap.