In some cases, we need to set CreatedDate for records.
setCreatedDate() method can be used to set CreatedDate for records.
For Example : Set createddate for a contact in test class
Contact con = new Contact() con.FirstName=’Firstname’; con.LastName=’LastName’; insert con; Test.setCreatedDate(con.Id, datetime.newInstance(2014, 9, 15, 12, 30, 0)
//After setCreatedDate method you can requery to check created date
Contact cont = [Select Id,Name,CreatedDate From Contact Where Id=:con.Id]; System.Debug(‘Created date=>’+cont.CreatedDate);//output:.CreatedDate=>2014-09-15 17:30:00
Note : Please make sure setCreateDate() method is called before Test.start method.
Don’t use seeAllData=true. setCreateDate() does not work with seeAllData=true.
Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at salesforce@webners.com