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
KevinRussellKevinRussell 

Count records having date within the past year

I'm trying to count records having a date within the past year.  I'm having trouble with the date comparrison.

My error: Compile Error: unexpected token: 'CutoffDate' at line.........

 

I don't know how to compare the DateTime record field: Start_Date_Time__c against a date variable.

 

Thanks for any help.

 

Kevin

 

trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

    date todaysDate = date.today();
    date CutoffDate = todaysDate.addDays(-365);

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > CutoffDate ];    

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Add a colon in front of your date binding variable like so:

 

trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

    date todaysDate = date.today();
    date CutoffDate = todaysDate.addDays(-365);

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > :CutoffDate ];    

 

All Answers

Sean TanSean Tan

Add a colon in front of your date binding variable like so:

 

trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

    date todaysDate = date.today();
    date CutoffDate = todaysDate.addDays(-365);

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > :CutoffDate ];    

 

This was selected as the best answer
ForcepowerForcepower
Kevin,

You're just missing a : before the variable CutoffDate. Need that since it's a variable within Apex rather than an object column/field.

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > :CutoffDate ];
KevinRussellKevinRussell
Thank you Sean Tan and Ram, the ":" did the trick.