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
naveen reddy 19naveen reddy 19 

How to give our own LastModifiedaDate for records while insetring?

Hi All,

  How can we give our own custom LastModifiedaDate  to records while inserting in test classes.

We have class which has a query

  
if (jobType == PROCESS_DEL_VIOC_FRANCHISE_RECORDS) {
            query = Database.getQueryLocator([SELECT Id FROM Lead  where RecordType.Name=:RecordTypeUtil.VIOC_Franchise_Web_lead and LastModifiedDate < LAST_N_YEARS:3]);
        }

While writing test class for this class, we are trying to create some test data. To meet above condition we have to give LastModifiedDate  which must be 3 years old. How can we write a test class in this situations.

Any help is really appreciated.

Thanks,
Naveen.
C. Praveen kumarC. Praveen kumar
There are 2 ways in which you can set system protected fields for test code coverage:

a) Using Test.loadData()
b) Using JSON.deserialize()
 
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. 
[Attaching a sample .csv file with the article to try the below example] 
 
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); 
 

MoggyMoggy
I believe your provided code will not satisfy the request, you would need to take out the update calls as the query is looking for Last Modified older than 3 years, meaning your update will bring it up to today, no matter how you created the case 

Aside that the given query is on lead not on case

so i would rplace the object , take out the update call and trial and error :-)