Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I currently have a SOQL statement which looks like so:

SELECT
   UPC__c
FROM
   Item_Status__c
WHERE
   Status__c in ('Void', 'OOS')
   AND Not_authorized__c = false
   AND (Response_Begin_Code__c = 'E' AND Response_Status_Code__c = 'NA')

But I actually want to select all records which DO NOT meet the criteria of the last condition. So I want all records where NOT (Response_Begin_Code__c = 'E' and Response_Status_Code__c = 'NA')

How do I write that in SOQL?

I tried using both NOT before the compound condition:
AND NOT (Response_Begin_Code__c = 'E' AND Response_Status_Code__c = 'NA')

and the negation operator !:
AND ! (Response_Begin_Code__c = 'E' AND Response_Status_Code__c = 'NA')

But both were not permitted.

Now I do know that logically I can rewrite my condition by negating each test and changing the AND to an OR like so:

AND (Response_Begin_Code__c != 'E' OR Response_Status_Code__c != 'NA')

but would prefer not to if I can avoid it, since I think it is less apparent what is being asked. I would prefer to be able to negate the entire compound condition.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

Add parentheses around your NOT.

SELECT
   UPC__c
FROM
   Item_Status__c
WHERE
   Status__c in ('Void', 'OOS')
   AND Not_authorized__c = false
   AND (NOT (Response_Begin_Code__c = 'E' AND Response_Status_Code__c = 'NA'))
share|improve this answer

Simply turn:

AND (Response_Begin_Code__c = 'E' AND Response_Status_Code__c = 'NA')

into:

AND (Response_Begin_Code__c != 'E' OR Response_Status_Code__c != 'NA')
share|improve this answer
2  
This isn't correct. In boolean algebra x ^ y = ¬(¬x v ¬y). So the reverse should be Response_Begin_Code__c != 'E' OR Response_Status_Code__c != 'NA'. –  Alex Tennant May 29 at 15:04
    
I understand the boolean algebra, but I had thought he was actually asking something slightly different. I updated my answer to be correct. –  dphil May 29 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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