2

I am trying to save the following query:

SELECT Id My_Object__c
WHERE Parent__c = :parentId
AND Start_Date__c > TODAY
OR (
    Start_Date__c <= TODAY AND End_Date__c >= TODAY
)

This seems like a valid query to me but I get the error:

expecting right square bracket, found 'OR'

However, if I cahnge OR to AND, it compiles.

SELECT Id My_Object__c
WHERE Parent__c = :parentId
AND Start_Date__c > TODAY
AND (
    Start_Date__c <= TODAY AND End_Date__c >= TODAY
)

Is this a bug? What am I missing here?

2

2 Answers 2

4

The problem is one of logic. The system is correct: You are not telling it what you want.

Basically, which is the first half of your OR statement? The start date alone, or the parent AND the start date?

EITHER put:

SELECT Id My_Object__c
WHERE (Parent__c = :parentId
AND Start_Date__c > TODAY)
OR (
    Start_Date__c <= TODAY AND End_Date__c >= TODAY
)

OR put

SELECT Id My_Object__c
WHERE Parent__c = :parentId
AND (Start_Date__c > TODAY
OR (
    Start_Date__c <= TODAY AND End_Date__c >= TODAY
))
1
  • The latter yes. Managed to fix the bracketing before seeing this, but it still wasn't immediately clear why the query was invalid. I guess it is more ambiguous than I assumed.
    – Adrian Larson
    Jun 3, 2015 at 20:30
-1

maybe put Start_Date__c>Today in braces ? -

SELECT Id My_Object__c WHERE Parent__c = :parentId AND (Start_Date__c>Today) OR ( Start_Date__c <= TODAY AND End_Date__c >= TODAY )

Can you try this ?

2
  • Hey Sanjay, your questions and answers will probably be received more positively if you put more work into their formatting, regardless of their content.
    – Adrian Larson
    Jun 3, 2015 at 22:05
  • Sure, will keep that in mind. Thanks for letting me know !
    – Sanjay
    Jun 3, 2015 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .