Print this page

error: System.UnexpectedException: Query is either selecting too many fields or the filter conditions are too complicated

Knowledge Article Number 000176969
Description Issue:
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:
 
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);
 
2) Receive Error:
15:52:40.201 (201678000)|EXCEPTION_THROWN|[10]|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 


 




promote demote