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
gbarger4581gbarger4581 

Help with test class when I need to set CreatedDate

I wrote a schedulable class that my company wants in order to send notifications about records that were submitted for approval more than 30 days ago, but still are not approved. My class works fine, but I cannot figure out how to write a test class for this. I need to be able to set the CreatedDate for these records because I want the test class CreatedDate to be more than 30 days old. Below is the query I use to build my list, but I can't create a test class where I insert and submit records for approval more than 30 days ago.

 

        ProcessInstanceWorkItem[] KYC = [Select processinstance.targetobjectid,actor.email
        from ProcessInstanceWorkItem where CreatedDate != LAST_N_DAYS:30 order by actor.email];
Ispita_NavatarIspita_Navatar

I don't think you can create records wherein you can set the created date. The workaround would be to have data in the org which satisfies the condition and you select such data and do the processing in your test class.

 

Alternatively you have have the interval of 30 days in a global variable of a setting object custom object which is fetch in the code and the code run and structured accordingly. So you can set the interval of 30 day or less dynamically and thus create the data accordingly in your test class and run the code to satisfy your code requirements.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.