13

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();
0

2 Answers 2

27

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 and CPU 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 to NONE except for Apex, which was at DEBUG.

Long start = Datetime.now().getTime();
for (Integer i = 0; i < 100; i++)
    Map<String, SObjectType> describe = Schema.getGlobalDescribe();
system.debug(Datetime.now().getTime() - start);
// divide above number by 100 (or whatever number of iterations you use) to get ms elapsed

Integer heapBefore = Limits.getHeapSize();
Map<String, SObjectType> describe = Schema.getGlobalDescribe();
system.debug(Limits.getHeapSize() - heapBefore);
// divide the above by Limits.getLimitHeapSize(); for percentage of heap used

Consumption

  1. Schema.getGlobalDescribe()
    CPU Time: ~7ms
    Heap Space: ~0.3%
  2. Schema.SObjectType.getDescribe()
    CPU Time: ~0.002ms
    Heap Space: ~0.001%
  3. Schema.SObjectField.getDescribe()
    CPU Time: ~0.38ms
    Heap Space: ~0.001%
  4. Schema.DescribeSObjectResult.getRecordTypeInfosByName()
    CPU Time: ~0.2ms
    Heap 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.

7
  • This is exactly what I'm looking for. If I'm thinking about writing a code base with lots of dynamic apex, how does that scale to the CPU /Heap limits? May 18, 2016 at 15:12
  • 1
    @user11235813 I started typing it out as a comment and then realized I should just edit my post with ramifications. Let me know if that's what you're trying to get at.
    – Adrian Larson
    May 18, 2016 at 15:35
  • YES! this is exactly what i'm looking for! Where are you getting this from?? May 18, 2016 at 20:10
  • @user11235813 Edited again to reflect the approach.
    – Adrian Larson
    May 18, 2016 at 20:23
  • Hi @AdrianLarson, the CPU time is significantly high in my orgs. Average CPU time for Schema.getGlobalDescribe() is ~57ms in sandbox and ~41 in production. Even in my developer edistion instance, the average CPU time is ~22ms. Are you aware of any such cases?
    – Avinash
    Jul 19, 2017 at 21:12
6

There is no limit for Describe Call in Salesforce.

As per the below document, There were few limits before Summer'14 release and it was remove post Summer'14 release.

For more information:- https://developer.salesforce.com/releases/release/Summer14/New+Apex+Enhancements

You must log in to answer this question.

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