function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
karthikeya 2karthikeya 2 

Created Date Over ride?

Hi all,

How to over ride the created date in the test class as past date.
Can any one he me providing the code.

Thanks in advance.
Best Answer chosen by karthikeya 2
karthikeya 2karthikeya 2
works in spring 16 onwords

Test.setCreatedDate(objCusWct.id, yesterday);

All Answers

LakshmanLakshman
CreatedDate is a system protected field, to override CreatedDate you will have to do by 2 ways:

1. Using Test.loadData. 
 
In this option you will need to create a .csv file and enter the necessary data in the .csv file. Once the csv file is ready, upload the csv file as a static resource. 
the Sample CSV used in the blow code is here - https://www.dropbox.com/s/7vnnu4draf1db2q/testCases.csv?dl=0
 
a. Click Develop | Static Resources, and then New Static Resource. 
b. Name your static resource testCases. 
c. Choose the file you just created. 
d. Click Save. 
 
Then Call Test.loadData in a test method to populate the test case data. 
 
Code Example: 
@isTest 
private class caseUtil{ 
static testmethod void testLoadData(){ 
List<sObject> ls = Test.loadData(Case.sObjectType,'testCases'); 
 
Case c = (Case)ls[0]; 
 
System.assert(ls.size() == 1); 
String cStatus = c.Status; 
DateTime cDate = c.CreatedDate; 
 
System.debug('Case Id: ' + c.Id); 
System.debug('Case Status: ' + cStatus); 
System.debug('Case Date: ' + cDate); 
 
c.status = 'New'; 
 
update c; 
 
System.debug('Case status: ' + c.status); 
}


 
2. Using JSON.deserialize 
You can create sObjects in memory with arbitrary CreatedDate values by using JSON.deserialize. This doesn't enforce the normal read-only field attributes that prevent you from setting a createdDate value. 
 
Code Example: 
 
@isTest 
private class CaseTest{ 
static testmethod void testLoadData(){ 
String caseJSON = '{"attributes":{"type":"CasSe","url":"/services/data/v25.0/sobjects/Case/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","Status":"Open","CreatedDate":"2012-10-04T17:54:26.000+0000"}'; 
Case c = (Case) JSON.deserialize(caseJSON, Case.class ); 
System.debug('Test case:' + c.createdDate); 
System.debug('Test caseId:' + c.Id); 
System.debug('Test caseStatus:' + c.status); 
 
Case c1 = new Case(); 
c1.Id = c.Id; 
c1.status = 'New'; 
update c1; 
 
System.debug('Test caseStatus1:' + c1.status); 
 
} 
}


If this helps, please help others by chosing it as an answer.

 
Rupali PophaliyaRupali Pophaliya
Hi,

Follow the steps given in this article to set created date on a record. https://help.salesforce.com/apex/HTViewSolution?id=000181873&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000181873&language=en_US)
VSK98VSK98
We can use Test.Setcreateddate() method in test class if it is spring 16 version.

Regards,
Siv.
 
karthikeya 2karthikeya 2
I need for the below version
 
karthikeya 2karthikeya 2
works in spring 16 onwords

Test.setCreatedDate(objCusWct.id, yesterday);
This was selected as the best answer