You need to sign in to do that
Don't have an account?
Derrick 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?
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?
Setting system protected fields for test code coverage
https://help.salesforce.com/articleView?id=Setting-system-protected-fields-for-test-code-coverage&language=en_US&type=1
Important warning if there are enabled triggers while loading test data:
https://salesforce.stackexchange.com/questions/103829/how-to-create-test-objects-with-lastmodifieddate-in-past
Best regards
Alain
You can set Created date while inserting the test data into Test class and this created date will be lastmodified date.
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.