CreatedDate, LastModifiedDate, and Id cannot be set using normal Data Manipulation Language (DML) operations in test classes. For example, attempting to set CreatedDate directly on a new record throws the error: "Save error: Field is not writable: Case.CreatedDate". This article describes two methods to set system-protected field values in test code when using API version 24 or later. Test.loadData() method loads data from a CSV file stored as a Static Resource. Because the data is loaded from a file rather than via DML, system-protected fields can be set.CreatedDate).testCases and upload your CSV file. Click Save.Test.loadData(Case.sObjectType, 'testCases') to load the records.List<sObject> which can be cast to the target sObject type. The loaded records will have the system-protected field values from the CSV.Test.loadData, cast the first result to Case and access c.CreatedDate to verify the loaded date value.@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);
}
}
JSON.deserialize() method creates sObjects in memory using JSON deserialization. This approach bypasses the normal read-only field restrictions that prevent setting CreatedDate in DML."CreatedDate":"2012-10-04T17:54:26.000+0000").JSON.deserialize(jsonString, Case.class) to create the sObject instance in memory.CreatedDate or other read-only fields without needing a full Static Resource setup.@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);
}
}
000386257

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.