0

My OR condition with in the where statement is failing to execute, Can someone let me know if I'm missing something or querying it wrong. Any help is much appreciated.

SELECT Id FROM Object__c
WHERE ( Latitude__c >= 3 AND Longitude__c>= -8 AND Latitude__c <= 4 AND Longitude__c <= 8 AND RecordtypeId =: recid AND Active__c = true ) OR (Statewide_Service__c = \'True\' AND State__c =: stateofcand AND RecordtypeId =: recid AND Active__c = true )Order by Resource_Type__c asc

1
  • n.b. these types of problems are best resolved using the Query Editor in Developer Console, SFDC workbench, Eclipse IDE, or other tool at your disposal. You develop the query up in parts, verifying each clause is doing what you expect
    – cropredy
    Dec 22, 2014 at 18:19

1 Answer 1

1

Does you first set of ANDs contradict themselves for the Latitude? I mean you cannot have one that is >= 3 AND <=4, basically you are saying either 3 or 4

I would rewrite it as follows:

SELECT Id FROM Object__c WHERE ( ( ( Latitude__c >= 3 AND Longitude__c>= -8) OR (Latitude__c <= 4 AND Longitude__c <= 8) ) OR (Statewide_Service__c = \'True\' AND State__c =: stateofcand) ) AND RecordtypeId =: recid AND Active__c = true Order by Resource_Type__c asc

Also is Statewide_Service__c a text field? If it is checkbox then do not include true in quotes

If you are concerned about the records not being returned try with just the or part in the query and see if anything is returned.

SELECT Id FROM Object__c WHERE Statewide_Service__c = \'True\' AND State__c =: stateofcand AND RecordtypeId =: recid AND Active__c = true Order by Resource_Type__c asc

1
  • May be he wants to use Between functionality by specifying range like Latitude__c >= 3 and Latitude__c < 5.
    – Mahmood
    Dec 22, 2014 at 6:47

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.