function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nickname7Nickname7 

Issue with too many field describes error

Hi,

 

I have an organization that has more than 100 custom objects. I need to loop through these objects and check if the object includes a specific field. As I'm doing this I hit the governor limit of field describes. I don't believe that I'm doing a field describe but it thinks I am. Here is my code that gets executed for each object (sObjectDescribe):

 

Map<String, SObjectField> fieldMap = sObjectDescribe.fields.getMap();
List<SObjectField> fieldValues = fieldMap.values();
for(SObjectField fieldValue:fieldValues)
{ 
String fieldValueStr = String.valueOf(fieldValue);
if (fieldValueStr.endsWith('__Special_Field__c') || fieldValueStr == ('Special_Field__c'))
{
return true;
}
}

 When I look at the documentation it talks about the sObjectDescribe.fields.getMap() call as follows:

 

The value type of this map is not a field describe result. Using the describe results would take too many system resources. Instead, it is a map of tokens that you can use to find the appropriate field. After you determine the field, generate the describe result for it.

 

So, what am I doing wrong here? Is this a bug or documentation bug?

 

Thanks!

 

 

AmitSahuAmitSahu

Try this and let me know if that help......

 

Map<String, SObjectField> fieldMap = sObjectDescribe.fields.getMap();
Set<String> fieldValues = fieldMap.KeySet();
for(String fieldValue:fieldValues)
{ 
String fieldValueStr = String.valueOf(fieldValue);
if (fieldValueStr.endsWith('__Special_Field__c') || fieldValueStr == ('Special_Field__c'))
{
return true;
}
}

 

Nickname7Nickname7

Hi,

 

I tried this but it didn't make a difference. Thanks for the suggestion though.

AmitSahuAmitSahu

Can we have the Error along with rest of the code ?

 

 

Nickname7Nickname7

The error is as follows:

 

Too many fields describes: 101

 

The code is as follows:

 

Set<string> objectNames = globalDescribeObject.keySet();
      for(string objectName:objectNames)
      {
          if(objectName.endsWith('__c'))
          {
                Schema.SObjectType  sObjectType = globalDescribeObject.get(objectName);
                Schema.DescribeSObjectResult sObjectDescribe = sObjectType.getDescribe();
          
                if(isSpecialFieldExists(sObjectDescribe)) 
                {
                    dropDownList.add(new Selectoption(sObjectDescribe.getName(),sObjectDescribe.getLabel()));

                }
          }
      }

public Boolean isSpecialFieldExists(Schema.DescribeSObjectResult sObjectDescribe)
   {
        Map<String, SObjectField> fieldMap = sObjectDescribe.fields.getMap();
        Set<String> fields = fieldMap.keySet();
        for(String field:fields)
        {   
            if (field.endsWith('__Special_Field__c') || field == ('Special_Field__c'))
            {
                return true;
            }
        }
        return false;
   }

 I can run a unit test on the above code and the field describe limit is consumed as many custom objects I have in the org. I tried it in different orgs and got the same result. In an org where we have more than 100 custom objects, this is blowing up with the above error.



AmitSahuAmitSahu

Just to test I tried the code below and could hit the limit due to describe in a loop.:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Set<string> objectNames = gd.keySet();
for(String strObj:gd.KeySet())
{
Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
 if(isSpecialFieldExists(r))
 {
     System.Debug('@@'+True);
 }

}

public Boolean isSpecialFieldExists(Schema.DescribeSObjectResult sObjectDescribe)
   {
        Map<String, SObjectField> fieldMap = sObjectDescribe.fields.getMap();
        Set<String> fields = fieldMap.keySet();
        for(String field:fields)
        {   
            if (field.endsWith('__Special_Field__c') || field == ('Special_Field__c'))
            {
                return true;
            }
        }
        return false;
   }

 This line of code is counted every time your loop runs:

 Map<String, SObjectField> fieldMap = sObjectDescribe.fields.getMap();

 

I hope that helps....