Loading

Set system protected fields for test code coverage

Publiseringsdato: Jun 24, 2026
Beskrivelse
In Salesforce Apex, system-protected fields such as 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.
Løsning

Approach 1: Using Test.loadData()

The 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.
Steps:
  1. Create a CSV file containing the test data including the desired system-protected field values (e.g., CreatedDate).
  2. Navigate to Setup > Develop > Static Resources and click New Static Resource.
  3. Name the resource testCases and upload your CSV file. Click Save.
  4. In your test method, call Test.loadData(Case.sObjectType, 'testCases') to load the records.
The method returns a List<sObject> which can be cast to the target sObject type. The loaded records will have the system-protected field values from the CSV.
Example: In your test class, after calling 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); 
    }
}

Approach 2: Using JSON.deserialize()

The 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.
Steps:
  1. Construct a JSON string representing the sObject, including the system-protected field values you need (e.g., "CreatedDate":"2012-10-04T17:54:26.000+0000").
  2. Call JSON.deserialize(jsonString, Case.class) to create the sObject instance in memory.
  3. Use the deserialized object in your test assertions. Note: This approach creates the object in memory only — you cannot insert it via DML without losing the protected field values.
This approach is useful when you need to test logic that depends on 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); 

    } 
}
Knowledge-artikkelnummer

000386257

 
Laster
Salesforce Help | Article