1

Can anyone whats wrong with this Query

 SELECT Id,name,Email,MobilePhone,MailingCity FROM Contact WHERE  Key_Skills__c = 'C++' OR  MailingCity = 'Austin' AND ( Id Not IN : applicantId ) LIMIT 1000

it gives an error

unexpected token: AND

I am generating this query dynamically in apex

Thanks

| improve this question | | | | |
4

You can't mix the OR and AND operators with out parenthesis. The working query will be:

SELECT Id, name, Email, MobilePhone, MailingCity 
FROM Contact 
WHERE (Key_Skills__c = 'C++' OR MailingCity = 'Austin') 
       AND ( Id Not IN : applicantId ) 
LIMIT 1000
| improve this answer | | | | |
2

Logically speaking even we cant say what is 1 OR 2 AND 3 is ?

So you need to make it clear:

SELECT Id,name,Email,MobilePhone,MailingCity FROM Contact WHERE  (Key_Skills__c = 'C++' OR  MailingCity = 'Austin') AND ( Id Not IN : applicantId ) LIMIT 1000
| improve this answer | | | | |
  • Operator precedence is usually the way that such ambiguity is resolved. In Apex, the && operator takes precedence over the || operator, so it would be evaluated as 1 || (2 && 3). It appears that SOQL operators don't have a similar precedence structure though. Definitely seems like one of those things that should be mentioned in the documentation (though it doesn't appear to be). – Derek F Jul 24 '19 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.