New Batch#100 (10th Nov 2021) - Salesforce Admin + Dev Training (WhatsApp: +91 - 8087988044) :https://t.co/p4F3oeQagK

Friday, 10 August 2012

How to pull values of records modified 7 days ago?

Question:
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.

Solution: 
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];

No comments:

Post a Comment

Labels