Loading

SOQL error 'QUERY_TOO_COMPLICATED'

Date de publication: Oct 13, 2022
Description

If a SOQL query exceeds the character limit of 100K, you might run into errors. Here are few sample scenarios where this can be the issue,

Formula fields may appear to be simple in the SOQL Query but when SQL is generated, the same formula fields may become complicated. Many such formula fields in combination may cause this error. 
Error: “QUERY_TOO_COMPLICATED: Query is either selecting too many fields or the filter conditions are too complicated.”

This same query limitation might also cause a Lightning Experience record page layout to fail with the below error instead.
Error: "We couldn't find the record you're trying to access. It may have been deleted by another user, or there may have been a system error. Ask your administrator for help."

Lightning Experience uses auto-generated SOQL to retrieve fields for a record page layout, so this could occur even when there is no customer-written SOQL. You might run across this limitation when you have a significant number of fields (likely greater than 250) in the page layout or many complex formula fields.
 
The character limit can also be hit by including a large number of currency fields, as they require the SOQL to use a format method, increasing the characters used by at least double the field API name length for each currency field.

 
Résolution
Formula fields can become complicated at runtime if there is a heavy calculation or if there are many formulas running at the same time. 
The resolution would be to simplify the query by removing the formula fields from the Query and fetch non-formula fields only. Trial and error will help identify the offending formula fields. 

Additionally, reducing the characters used in the API names of each field can also help you in resolving the issue. 

'System UnexpectedException' error when getDescribe() is used
Numéro d’article de la base de connaissances

000386662

 
Chargement
Salesforce Help | Article