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.
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)System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicatedThis error occurs because Salesforce builds and optimizes SQL internally in two different ways depending on the execution context:
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.
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.
000386129

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.