Salesforce | Set CreatedDate for s-object records in test class

Author - Webner
27.07.2016
|
0 Comments
||

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

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *