How to set CreatedDate for records in Test Class in Salesforce?

How to set CreatedDate for records in Test Class in Salesforce?

setCreatedDate() can be used to set CreatedDate for records in Test Class in Salesforce.

Sample Code:

        Account a = new Account(name=’myAccount’);
        insert a;
        Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));

Note:

1. Don’t use seeAllData=true.

2. Make sure setCreateDate method is called before Test.start method.

Cheers!!!

Leave a Reply