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
Christos KolonisChristos Kolonis 

How to compare two date fields in apex, event date and today

Hello, 

I need to compare two date fields, the End Date of an event created inside an Opportunity and the today date. Based on the outcome, i will update an Opportunity Stage.

I tried this unsuccessfully:
 
Event eve = [select id, subject, whatId, EndDate from Event where whatId =: newOpp.id];
                date d = eve.EndDate;
                system.debug(d);
                
                date e = system.today();
                system.debug(e);
                
                boolean dates = d.isSameDay(e);
                system.debug('Dates: ' + dates);




if(newOpp.RecordTypeId == recordTypeServices && dates == false && newOpp.StageName == 'Proposal'){
                   newOpp.addError('The appointment with the customer must be completed'); 
                }

That is not working. Am i missing something?

Thank you!
ANUTEJANUTEJ (Salesforce Developers) 
Hi Christos,

I see that you have put multiple debug statements in the code while running have you checked where the code is failing? let us know the issue you are facing to check further.

Looking forward to your response.

Thanks.
Christos KolonisChristos Kolonis
Hello ANUTEJ,

Well the goal is when the end date of event is the same as the today date, the system will allow me to go to 'Proposal' opportunity stage. However when the end date of the even is not the same as the today date will not allow me to select the 'Proposal' stage. So i created that if statement but when i try to run what have already described is not working. My thought is that the dates == false is wrong and it must be another way to fix it. The system.debug('Dates: ' + dates); is returning the proper boolean based on the dates i'm providing. I don't know how else could i compare those two dates.