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
Sfdc SaleforceSfdc Saleforce 

How to Change CreatedDate Of Record In Test Class

How to Change CreatedDate Of Record In Test Class
Best Answer chosen by Sfdc Saleforce
ravi soniravi soni
hi Sfdc,
Opportunity opp = new Opportunity();
        opp.name = 'Test Opportunity';
        opp.stageName = '1 Initial Sales Activity';
        opp.CloseDate = System.today();
        insert opp;

 Datetime yesterday = Datetime.now().addDays(-1);
        Test.setCreatedDate(opp.Id, yesterday);



try above code. I believe it will work.
don't forget to mark it as best answer.
Thank you