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
jucuzoglujucuzoglu 

How to pull values of records modified 7 days ago

I want to create a list of sObjects that were modified 7 days ago.

 

I am using code similar to this, but when I get to the SOQL query it appears that something is not quite working.

 

Date myDate = date.today()-7;
List<Recruitment_Cycle__c> RCs = [Select ID, Name, LastModifiedDate from Recruitment_Cycle__c where LastModifiedDate =: myDate ]; 

In this case I am getting 0 rows, however there are a number of records that I would expect to meet the criteria when looking at the data. I suspect something is wrong with my comparison etc...

 

When I check the debug for myDate it is assigning what looks like the proper date.

Best Answer chosen by Admin (Salesforce Developers) 
kranjankranjan
Hi jucuzoglu,

One thing is that you should use date.today().addDays(-7)

And also you are using exact match "=" sign. And LastModifiedDate field also contains time, whereas you are just using today function which will return you Date part. So i do not think there will be a match. Please verify if you need Date or Date and Time. I would think you want that day. So you may have to use something like this:

Date myDateStart = date.today().addDays(-7);
Date myDateEnd = date.today().addDays(-6);
List<Recruitment_Cycle__c> RCs = [Select ID, Name, LastModifiedDate from Recruitment_Cycle__c where LastModifiedDate >=: myDateStart AND LastModifiedDate <=: myDateEnd];

Hope this helps.

All Answers

kranjankranjan
Hi jucuzoglu,

One thing is that you should use date.today().addDays(-7)

And also you are using exact match "=" sign. And LastModifiedDate field also contains time, whereas you are just using today function which will return you Date part. So i do not think there will be a match. Please verify if you need Date or Date and Time. I would think you want that day. So you may have to use something like this:

Date myDateStart = date.today().addDays(-7);
Date myDateEnd = date.today().addDays(-6);
List<Recruitment_Cycle__c> RCs = [Select ID, Name, LastModifiedDate from Recruitment_Cycle__c where LastModifiedDate >=: myDateStart AND LastModifiedDate <=: myDateEnd];

Hope this helps.
This was selected as the best answer
JitendraJitendra

Hi,

 

You are comparing Date with Datetime and thats why you got problem.

 

Please try below code, although i have not tested it you must get what i am trying to perform here:

 

Date myDate = date.today()-7;
Datetime startDateTime= datetime.newInstance(myDate.year(), myDate.day(), myDate.month());
Datetime endDatetime = myDateTime.addDays(1);

List<Recruitment_Cycle__c> RCs = [Select ID, Name, LastModifiedDate from Recruitment_Cycle__c where LastModifiedDate >= :startDateTime AND  LastModifiedDate <= :endDatetime];