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
amyer2240amyer2240 

Compile Error: unexpected token: '<' in date comparison

Here's a snippet of my code where I'm getting this error:

for(List<Opportunity> opportunities : [Select Id from Opportunity]) 
    {for(Opportunity o : opportunities) 
    {if(o.CloseDate < :Date.valueOf('2011-01-01') && o.StageName = 'Received');}}
Map<Id, Opportunity> opportunitiesById = new Map<Id, Opportunity>(opportunities);

I'm getting the error on the {if(o.CloseDate < :Date.valueOf('2011-01-01') snippet. How can I do a date comparison without the < ?

Thank you!!
Best Answer chosen by amyer2240
Jean-NoelJean-Noel
In you code sample, there is still a small error, you'll need to replace o.CloseDate < :Date.valueOf with o.CloseDate < Date.valueOf.
There is no : before Data.ValueOf. The sign : is only used in query when you want to use external variable.

for(List<Opportunity> opportunities : [Select Id, CloseDate, StageName, from Opportunity])
    {for(Opportunity o : opportunities)

    {if(o.CloseDate < Date.valueOf('2011-01-01') && o.StageName == 'Received');}}

Map<Id, Opportunity> opportunitiesById = new Map<Id, Opportunity>(opportunities);

All Answers

Jean-NoelJean-Noel
You will run in multiple problem with that code.

You don't need the : before date.Valueof, you try also to test stagename, it will require a == and not only =.
To test you must use ==, to affect a value it's =

Here a quick sample.

for(Opportunity o : [Select Id, CloseDate, StageName from Opportunity])
{
    if(o.CloseDate < Date.valueOf('2011-01-01') && o.StageName == 'Received')
    {
  // Do something       
    }
}
amyer2240amyer2240
I'm really new to Apex, so thanks in advance for your patience! I'm trying to create the for loop on a list so I can pull that list into the map a few lines below. Can I still do that using the code you provided? With the changes you suggested I have: 
for(List<Opportunity> opportunities : [Select Id, CloseDate, StageName, from Opportunity]) 
    {for(Opportunity o : opportunities) 
    {if(o.CloseDate < :Date.valueOf('2011-01-01') && o.StageName == 'Received');}}
Map<Id, Opportunity> opportunitiesById = new Map<Id, Opportunity>(opportunities);

But I'm still getting the error with the '<' sign. 
Jean-NoelJean-Noel
In you code sample, there is still a small error, you'll need to replace o.CloseDate < :Date.valueOf with o.CloseDate < Date.valueOf.
There is no : before Data.ValueOf. The sign : is only used in query when you want to use external variable.

for(List<Opportunity> opportunities : [Select Id, CloseDate, StageName, from Opportunity])
    {for(Opportunity o : opportunities)

    {if(o.CloseDate < Date.valueOf('2011-01-01') && o.StageName == 'Received');}}

Map<Id, Opportunity> opportunitiesById = new Map<Id, Opportunity>(opportunities);
This was selected as the best answer
amyer2240amyer2240
Perfect! Thank you so much.