---
schema_version: "1"
id: f2c3407030499f373fefb143fc44ef963c2f7b880707b408784436f937c8dcba
title: "ApexGuru Antipattern: Schema.getGlobalDescribe() In a Loop"
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_in_loop.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_schema_getglobaldescribe_in_loop.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 2a768691eeb52c39f85094c0d69fbd39180da1a85a4cdc304a607fb4a3acdfda
description: The Schema.getGlobalDescribe() method collects schema information on all SObjects, which is computationally expensive. Using Schema.getGlobalDescribe() inside a loop can lead to performance issues, including breaking governor limits.
products:
  - Cross Cloud
release: "264"
---
# ApexGuru Antipattern: Schema.getGlobalDescribe() In a Loop

The Schema.getGlobalDescribe() method collects schema information on all SObjects, which is computationally expensive. Using Schema.getGlobalDescribe() inside a loop can lead to performance issues, including breaking governor limits.

## Scope

Detection and recommendation

## Detection Example

```java
apex
public class ObjectMetadataHandler {
    
    public void processObjectNames(List<String> objectNames) {
        for (String objectName : objectNames) {
            if (Schema.getGlobalDescribe().containsKey(objectName)) {
                Schema.SObjectType sObjectType = objectMap.get(objectName);
                System.debug('Object found: ' + objectName);
            } else {
                System.debug('Object not found: ' + objectName);
            }
        }
    }
}
```

## Recommendation

Cache the results of `Schema.getGlobalDescribe()` outside of the loop and reuse them within the loop.

```java
public class ObjectMetadataHandler {
    // Cache the result of Schema.getGlobalDescribe() to avoid redundant calls
    private static Map<String, Schema.SObjectType> objectMap = Schema.getGlobalDescribe();

    public void processObjectNames(List<String> objectNames) {
        for (String objectName : objectNames) {
            // Use the cached objectMap to access SObjectType
            if (objectMap.containsKey(objectName)) {
                Schema.SObjectType sObjectType = objectMap.get(objectName);
                System.debug('Object found: ' + objectName);
            } else {
                System.debug('Object not found: ' + objectName);
            }
        }
    }
}
```

#### See Also

- [getGlobalDescribe() in the Apex Reference Guide](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_schema.htm#apex_System_Schema_getGlobalDescribe)