CreatedDate in test classes

Finally Salesforce fills the gap and allows you to specify the createddate field within test classes. You only have to use the Test.setCreatedDate method introduced with Spring ’16.

@isTest
private class CreatedDateTest {

static testMethod void test() {

Contact c = new Contact(FirstName ='Marc', LastName = 'Benioff');
insert c;

Test.setCreatedDate(c.Id, DateTime.newInstance(2016, 1, 26));

Test.startTest();

Contact myContact = [SELECT CreatedDate FROM Contact LIMIT 1];
System.assertEquals(myContact.CreatedDate, DateTime.newInstance(2016, 1, 26));

Test.stopTest();
}

}

Better late than never!

Leave a Reply

avatar
  Subscribe  
Notify of