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
Clap MasterClap Master 

Create CaseHistory entry through Apex

I'm creating cases in a test class to do unit testing, and I need to have the test cases also have some case history items.  When you edit a case through the UI, we have 'Status' as a tracked field, so any cahnges are tracked in the CaseHistory table.  When I cahnge the 'Status' programatically however, through Apex, it does not create an extry in the CaseHistory table.

 

Does anyone know how to accomplish this?

crop1645crop1645

1. Per the SFDC http://www.salesforce.com/us/developer/docs/object_reference/index.htm xxxHistory objects are readOnly. You can't do DML inserts on these objects

 

2. And, yes, testmethods won't create xxxHistory objects either. Note: http://success.salesforce.com/ideaView?id=08730000000h6RwAAI

 

3. A way around this is to abstract your CaseHistory access using a CaseHistoryWrapper class. If test.isRunningtest() is false, then fetch CaseHistory using SOQL and populate the wrapper by copying from the actual CaseHistory.oldValue, newvalue, .. fields into public properties in your wrapper; If test.isRunningtest() is true, don't execute any SOQL and instead directly populate your public properties in the wrapper with known test data.