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
Victor19Victor19 

SOQL Unexpected token error for Date field

Hi,

I am trying to build a SOQL query and my condition is when Intake date in object 1 is greater than Start date in object 2 and lesser than End date n Object, then I will be getting the id of the record.  Please see my code below:

Object1 obj1 = trigger.new[0];
List<Object2> DateList = [Select id, Start_Date__c, End_Date__c FROM Object2 WHERE obj1.Intake_Date__c > Start_Date__c AND obj1.Intake_Date__c < End_Date__c];

Can someone please suggest a fix to my code?

Thanks!
Best Answer chosen by Victor19
GlynAGlynA
Looks like you're missing a couple of colons, and you have to put the field first in the comparisons:

Object1 obj1 = trigger.new[0];
List<Object2> DateList = [Select id, Start_Date__c, End_Date__c FROM Object2 WHERE  Start_Date__c < :obj1.Intake_Date__c AND  End_Date__c > :obj1.Intake_Date__c];

-Glyn

All Answers

izay.ramos-irizarry1.3893936724146558E12izay.ramos-irizarry1.3893936724146558E12
If Object 1 is the parent objetc of Object 2 then try;

[SELECT Id, Start_Date__c, Obj1__r.IntakeDate__c FROM Object2 WHERE obj1__r.Intake_Date__c < Start_Date__c AND obj1__rIntake_Date__c < End_Date__c ]
Victor19Victor19
Thanks for the suggestion izay!
There is a lookup relationship between both the objects. Object1 has a lookup to Object2 and Object1 is the standard Case Object.
I used your statement and it did not work!
GlynAGlynA
Looks like you're missing a couple of colons, and you have to put the field first in the comparisons:

Object1 obj1 = trigger.new[0];
List<Object2> DateList = [Select id, Start_Date__c, End_Date__c FROM Object2 WHERE  Start_Date__c < :obj1.Intake_Date__c AND  End_Date__c > :obj1.Intake_Date__c];

-Glyn
This was selected as the best answer
Victor19Victor19
Thanks GlynA! Worked just as I wanted it to!