function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Greg CooganGreg Coogan 

Error when querying with multiple conditions in the WHERE clause using AND

I am trying to run a query that has multiple conditions in the WHERE clause. If I reduce it to a single condition by removing two of the other conditions I need, the query works. I have three conditions and I have tried a query with each individual condition. They all work. However, when they are combined using AND, I receive an error. I have tried all different combinations of using parentheses to see if it makes a difference, but no luck. To provide an example, I have replaced the field names,object names, and values with fake names. I am using the developer console's query editor at this time.

Example:
SELECT SUM(CustomField1) FROM CustomObject__c WHERE CustomField1 = 'Value1' AND CustomField2 = 'Value2' AND CustomField3 != 'Value3'

I have referenced the SOQL documentation but no luck. Do you see anything wrong with my query?
Best Answer chosen by Greg Coogan
Greg CooganGreg Coogan
I was correct. I changed the queries to display in ANSI instead of UTF-8 in Notepad. It showed that it was different characters when decoded, but in UTF-8 they displayed the same. Sorry for bluring out field names and values - I beleive my work is propriety.

Queries in ANSI encoding

All Answers

JustAGirlyGeekJustAGirlyGeek
What is the error message you are getting?
Greg CooganGreg Coogan
"Unknown error parsing query" - don't think its very useful.
JustAGirlyGeekJustAGirlyGeek
I just tried a similar query with 3 conditions in my where clause and it worked. What field types are you using?
Greg CooganGreg Coogan
They are picklists. It doesn't say multi-select picklists. I assume it would say that in the list of custom fields if they were.
JustAGirlyGeekJustAGirlyGeek
Strange that it's not working for you when you use all 3 conditions at once. I just ran this query with all 3 conditions using custom picklist field and no errors:

SELECT SUM(Amount) FROM Opportunity WHERE QuoteBillingFrequency__c = 'Monthly' AND Currency__c = 'USD' AND QuotePaymentMode__c != 'Credit Card'

Does it work with any combination of 2 fields?
Greg CooganGreg Coogan
It works with the following:
1. Condition 1 and 2
2. Condition 1 and 3
3. Condition 2 and 3

However, if I open the console, try my original query, and get the error, then try the query with Condition 2 and 3, its also fails. They, if I copy that query from the top of the console when it originally worked, it works again. I am stunned. I checked and the queries appear identical.

I opened the two queries (both have Condition 2 and 3 but only one of them works) in Notepad++. In that application, when you double-click on a word, it will show you other instances of the word. Strangely, the "AND" operator and field name of Condition 3 do not highlight as expected. It appears as if they are one word. I think it is displaying the same on the front end, but the back-end encoding may be different. (Potentially different Unicode characters)

This is the strangest thing I've ever seen. Its hard to explain, but I did the best I could.
Greg CooganGreg Coogan
I was correct. I changed the queries to display in ANSI instead of UTF-8 in Notepad. It showed that it was different characters when decoded, but in UTF-8 they displayed the same. Sorry for bluring out field names and values - I beleive my work is propriety.

Queries in ANSI encoding
This was selected as the best answer
JustAGirlyGeekJustAGirlyGeek
Strange indeed but happy it is (hopefully) now working for you.
Greg CooganGreg Coogan
Thanks for your help!