Loading

'System UnexpectedException' error when getDescribe() is used

Data pubblicazione: May 17, 2026
Descrizione

When using getSObjectType().getDescribe().fields.getMap().keyset() to dynamically retrieve all fields on an object (such as Opportunity) in Apex and then running a query with all those fields, the following error may be thrown:
System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated.
This error is thrown at runtime in the Apex Developer Console, even though executing the equivalent query directly in Workbench (via the API) does not produce the error.

 

Steps to Reproduce

  • Open the Salesforce Developer Console
  • Execute an Apex script that uses getSObjectType().getDescribe().fields.getMap().keyset() to retrieve all field names from the Opportunity object, builds a dynamic SOQL query string with all those fields, and runs Database.query(eventQuery)
  1. Opportunity Opp; String eventQuery=null; List<String> eventFields=new List<String>(); eventFields.addAll(Opportunity.getSObjectType().getDescribe().fields.getMap().keyset()); eventQuery='select '+eventFields.get(0); for(integer i=1;i<eventFields.size();i++){ eventQuery+=','+eventFields.get(i); } eventQuery+=' from Opportunity where id=\'006L00000030dw8\''; Opp=Database.query(eventQuery);
  • Observe the error: System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated
  • Run the same generated SOQL query directly in Workbench — notice that it succeeds without error
Risoluzione

Root Cause

This error occurs because Salesforce builds and optimizes SQL internally in two different ways depending on the execution context:

  • Via Apex: The internally generated SQL can reach up to 64,000 characters. When all fields of a large object like Opportunity are selected (which may include hundreds of custom and standard fields), the dynamically built query string often exceeds this 64K SQL limit.
  • Via API (e.g., Workbench): The same query is processed more efficiently and uses approximately 57,000 characters, allowing it to complete successfully.

The getDescribe().fields.getMap().keyset() pattern retrieves every single field on the object — including fields you may not need — which is why this error surfaces in Apex but not in the API context.

Solution

The recommended fix is to select only the specific fields you actually need rather than all fields on the object. This is a best practice in Apex development regardless of query limits.
If your use case genuinely requires fetching a large number of fields, an alternative approach is to break the field list into smaller batches, run multiple queries with a subset of fields each, and merge the resulting lists back into a single result set in Apex.
Note: While the documented SOQL query string length limit is 20,000 characters, the underlying SQL limit is 64,000 characters via Apex. This discrepancy means that dynamically built queries selecting all fields on complex objects can fail even if the SOQL string itself appears to be within documented limits.


 

Numero articolo Knowledge

000386129

 
Caricamento
Salesforce Help | Article