+ 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