error: System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated
|Knowledge Article Number||000176969|
Customer is using getDescribe on Opportunity while querying.
It does generate Unexpected exception: Query is either selecting too many fields or the filter conditions are too complicated.
Executing the query directly on workbench doesn’t throw any exceptions though.
Steps to Reproduce:
1. Go to workbench Or developer Console and execute the snippet:
List<String> eventFields=new List<String>();
eventQuery+=' from Opportunity where id=\'006L00000030dw8\'';
2) Receive Error:
15:52:40.201 (201678000)|EXCEPTION_THROWN||System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated.
15:52:40.201 (201948000)|FATAL_ERROR|System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated.
3) However, executing above query on workbench doesn’t give any exceptions. It does give the results.
|Resolution||This is working as designed, we build and optimize generated SQL differently depending on the context (Apex v API).
Because of this difference this results in hitting the SQL limit of 64k via Apex whereas we don't via the API (57K).
The solution here is to break up the query (or reduce the columns retrieved and only get what the really need which is always best practice).
User could probably do something like break up by X number of fields via the describe and merge the resulting lists back into one.
This would be more scalable than us increasing max SOQL.
Note: We say SOQL length can be up to 20K however the real limit is the SQL 64K