---
schema_version: "1"
id: 73b639c76d760679fe007ef56045b0f9e1b5c32ceb5bf1ce9927890cca485bd2
title: "ApexGuru Antipattern: Schema.getGlobalDescribe() Called Multiple Times Within a Class"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/apexguru_antipattern_schema_getglobaldescribe_called_multiple_times.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_schema_getglobaldescribe_called_multiple_times.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 075161431323262a782cc85fd64b3e987bbc0ef7b96efb51d95d291b086b6dac
description: Multiple calls to Schema.getGlobalDescribe() in a class cause performance issues and are computationally expensive. Calling this method multiple times within the same method results in redundant schema retrieval. Increased CPU usage can cause you to go over your governor limits.
products:
  - Cross Cloud
release: "264"
---
# ApexGuru Antipattern: Schema.getGlobalDescribe() Called Multiple Times Within a Class

Multiple calls to Schema.getGlobalDescribe() in a class cause performance issues and are computationally expensive. Calling this method multiple times within the same method results in redundant schema retrieval. Increased CPU usage can cause you to go over your governor limits.

## Scope

Detection and recommendation

## Detection Example

```java
public void pro(String objectName) {
    
    // Get the describe result for the provided object name
    Schema.DescribeSObjectResult describeResult = Schema.getGlobalDescribe().get(objectName).getDescribe();
    
    // Log object label and plural label
    System.debug('Object Label: ' + describeResult.getLabel());
    System.debug('Object Plural Label: ' + describeResult.getLabelPlural());

    // Dynamically create a new instance of the SObject
    SObject newRecord = Schema.getGlobalDescribe().get(objectName).newSObject();
    
    newRecord.put('Name', 'Address');
    
    // Insert the record
    insert newRecord;
    
    // Log the newly created record ID
    System.debug('New record created with ID: ' + newRecord.Id);
}
}
```

## Recommendation

When you optimize logic design, avoid multiple `Schema.getGlobalDescribe()` calls. Focus on optimizing the functionality itself. Use alternatives like caching or metadata to eliminate the need for these calls. Retrieve schema information one time at the start of the method and reuse it to improve performance.

```java
public void getObjectDescribe(String objectName) {    
    // Get the describe result for the provided object name
    Schema.DescribeSObjectResult describeResult = null
    try {
         List<Schema.DescribeSObjectResult> describes = Schema.describeSObjects(new String[] { objName}, SObjectDescribeOptions.DEFERRED);
         result = describes[0];
     } catch (InvalidParameterValueException ipve) {
          result = null;
     }
     Schema.DescribeSObjectResult objectResult = result;
    
    // Log object label and plural label
    
    if (objectResult != null) {
        System.debug('Object Label: ' + describeResult.getLabel());
        System.debug('Object Plural Label: ' + describeResult.getLabelPlural());
    }

    // Dynamically create a new instance of the SObject
    SObject newRecord = (SObject)Type.forName( objectName ).newInstance()
    
    newRecord.put('Name', 'Address');
    
    // Insert the record
    insert newRecord;
    
    // Log the newly created record ID
    System.debug('New record created with ID: ' + newRecord.Id);
}
```