You need to sign in to do that
Don't have an account?

How to set LastModifiedDate in test class
Hello - I need to set the LastModifiedDate in a test class to yesterday. The code below sets the created date to yesterday but the LastModifiedDate is still coming back as the current date/time. Any help would be appreciated.
USER_DEBUG [452]|DEBUG|last modified date (Lead:{Id=00Q7j000002lrl2EAA, CreatedDate=2021-02-03 22:27:47, LastModifiedDate=2021-02-04 22:27:46}, Lead:{Id=00Q7j000002lrl3EAA, CreatedDate=2021-02-03 22:27:47, LastModifiedDate=2021-02-04 22:27:47})
USER_DEBUG [452]|DEBUG|last modified date (Lead:{Id=00Q7j000002lrl2EAA, CreatedDate=2021-02-03 22:27:47, LastModifiedDate=2021-02-04 22:27:46}, Lead:{Id=00Q7j000002lrl3EAA, CreatedDate=2021-02-03 22:27:47, LastModifiedDate=2021-02-04 22:27:47})
Datetime yesterday = Datetime.now().addDays(-1); Test.setCreatedDate(testLeadTwo.Id, yesterday); Test.setCreatedDate(testLeadOne.Id, yesterday); Test.startTest(); Database.executeBatch(new SfToPcRetryUpdateRecords_BatchProcess()); DateTime x5Min = System.now().addMinutes(-5); List<Lead> ldCreated = [SELECT ID, CreatedDate, lastmodifieddate FROM Lead WHERE lastmodifieddate >= LAST_N_DAYS:1 AND lastmodifieddate < :x5Min]; System.debug('last modified date '+ldCreated);
Test.stopTest();
or you can follow the below steps
To create testdata with lastModifiedDate follow the below link
https://help.salesforce.com/articleView?id=000332070&type=1&mode=1
OR
Step 1- Create CSV File for your sobject
name createdDate lastmodifiedDate
Test1 2000-01-01T00:00:00Z 2001-01-01T00:00:00Z
Test2 2000-01-02T00:00:00Z 2001-01-02T00:00:00Z
Step 2 - save as Static Resource (named here as recordsWithLastModDateInPast)
Step 3 - Code test method