AvoidMultipleMassSchemaLookups Rule (Generally Available)

AvoidMultipleMassSchemaLookups is a path-based rule that detects scenarios where expensive schema lookups are made more than one time in a path and cause performance degradation.

AvoidMultipleMassSchemaLookups identifies these methods:

  • Schema.getGlobalDescribe()
  • Schema.describeSObjects(...)

Flagged lookups include:

  • Lookups within these types of loops: ForLoopStatement, ForEachLoopStatement, DoWhileStatement, and WhileLoopStatement
  • Multiple expensive schema lookups that are invoked
  • An expensive schema lookup that is executed multiple times

These common scenarios trigger a violation from AvoidMultipleMassSchemaLookups.

  • Schema.getGlobalDescribe() within a loop
  • Schema.describeSObjects(...) within a loop
  • Schema.getGlobalDescribe() preceding a Schema.getGlobalDescribe() or Schema.describeSObjects(...) method call anywhere in the path
  • Schema.describeSObjects(...) preceding a Schema.describeSObjects(...) or Schema.getGlobalDescribe() method call anywhere in the path

Definition 

Source

@AuraEnabled-annotated methods. @InvocableMethod-annotated methods. @NamespaceAccessible-annotated methods. @RemoteAction-annotated methods. Any method returning a PageReference object. public-scoped methods on Visualforce Controllers. global-scoped methods on any class. Messaging.InboundEmailResult handleInboundEmail() methods on implementations of Messaging.InboundEmailHandler. Any method targeted during invocation.

Sink

Schema.getGlobalDescribe(). Schema.describeSObjects(...).

Interpreting Results 

Match any violation message that you receive with these cases to understand more about the violation.

Loop Case 

Schema.getGlobalDescribe was called inside a loop. [ForEachStatement at AuraEnabledFls:27]

Your code calls Schema.getGlobalDescribe() or Schema.describeSObjects(...) inside a loop statement. To move the Schema.getGlobalDescribe() or Schema.describeSObjects(...) outside the loop, modify and then rescan your code.

Multiple Schema Lookups Are Invoked Case 

Multiple expensive schema lookups are invoked. [Schema.describeSObjects at AuraEnabledFls:27]

Your code invokes Schema.getGlobalDescribe() preceded by Schema.describeSObjects. Modify your code so that only one expensive schema lookup is invoked.

More Than One Execution in a Path Case 

Schema.getGlobalDescribe executed multiple times in the call stack. [getFields at AuraEnabledFls:27, getFields at AuraEnabledFls:28, getFields at AuraEnabledFls:29]

Schema.getGlobalDescribe or Schema.describeSObjects is executed multiple times in a single path. Reduce the execution of the method to one time, then rescan your code

End of Life

Salesforce Code Analyzer v4 is scheduled for retirement as of August 2025. Use Code Analyzer v5 instead.