+ Start a Discussion
Joseph Ucuzoglu 3Joseph Ucuzoglu 3 

Is there a SOQL string limit when using the database.query command?

I have some code that is returning a non-descript error when running a query string I am building. The string is built dynamicly and in this use case where it is breaking it is making multiple Geolocation comparisons which tend to be kind of verbose in SOQL and my theory is that the string is getting too long.

Is there a limit to the size of the SOQL string?
Gordon EngelGordon Engel
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select.htm

(relevant text below)

Statement Character Limit—By default, SOQL statements cannot exceed 20,000 characters in length. For SOQL statements that exceed this maximum length, the API returns a MALFORMED_QUERY exception code; no result rows are returned.

NOTE:

Long, complex SOQL statements, such as statements that contain many formula fields, can sometimes result in a QUERY_TOO_COMPLICATED error. The error occurs because the statement is expanded internally when processed by Salesforce, even though the original SOQL statement is under the 20,000 character limit. To avoid this, reduce the complexity of your SOQL statement.