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
Derrick Abbey 9Derrick Abbey 9 

How to set last modified date in apex test

Hello,

I have a scheduled apex class that uses the Last Modified Date in an SOQL query.  My problem is: when I create the Apex test class, I can't set the Last Modified Date to be in the past on the record that I am inserting for my test.  But when I do the insert it updates the Last Modified Date to the current date/time.  Does anyone know of a way of creating a test record where the created date and last modified date will be in the past?
Alain CabonAlain Cabon
Amit Singh 1Amit Singh 1
Hello,

You can set Created date while inserting the test data into Test class and this created date will be lastmodified date.
 
Derrick Abbey 9Derrick Abbey 9
Hi Alain,

Thank you for your response.  I'm trying to figure out if I can go back and set the last modfied date using the JSON.deserialize method.  I haven't had a whole lot of time to work on that though.

Hi Amit,

Thank you for your suggestion.  I had set the created and last modified dates when I did the insert of the test opportunity, however, there are other triggers that modify the opportunity, so those automatically update the opportunity and thus change the last modified date.  So hence my question.

I'm wondering if I need to change my test to SeeAllData = True so it can look at actual opportunities that haven't been modified in the past couple of weeks.