Test.setCreatedDate fails on note record created in test context

I've raised support case 13752643 for the problem with Test.setCreatedDate not working with a Note that was created in the test method transaction. I'll relay the important updates from the case here.

See Known Issue Test.setCreatedDate() does not work for Note object

Update: 17th October - The known issue is now marked as "Fixed - Winter '17"


Repo code against v36.0 on na5.

@IsTest
public class Test_SettingCreatedDateOnNote {

    @isTest
    public static void CleanUp_Test() {
        Account testAccount = new Account();

        testAccount.Name = 'Test Account';

        insert testAccount;

        System.assertNotEquals(null, testAccount.Id);

        Note testNote = new Note();

        testNote.Title = 'Test Account Note';
        testNote.Body = 'Test Account Note Body.';
        testNote.ParentId = testAccount.Id;

        insert testNote;

        System.assertNotEquals(null, testNote.Id);

        Test.setCreatedDate(testAccount.Id, DateTime.now().addMonths(-6));
        Test.setCreatedDate(testNote.Id, DateTime.now().addMonths(-6));

        Test.startTest();

        System.assert([SELECT COUNT() FROM Note] > 0);

        Test.stopTest();
    }

}

The line Test.setCreatedDate(testNote.Id, DateTime.now().addMonths(-6)); throws the following exception:

System.NoDataFoundException: The sObject with the ID 0027000000VBxllAAD isn’t part of this transaction. Provide the ID of an sObject that this test method created.


For argument's sake, let's assume SFDC will fix the issue with

Test.setCreatedDate(n.id,someDateTime)

but, until then, two workarounds

JSON deserialize

Note n = (Note) Json.deserialize('{"createdDate" : "2016-01-01T00:00:00Z",' +
                                   '"title" : "foo",' +
                                   '"body" : "fooBody",' +
                                   '"parentId" : "' + a.id + '"' +
                                  '}',
                                  Note.class);
insert n; 

Static property in some Util class

public static Date todayDate {
   get {return todayDate == null ? Date.today() : todayDate;}
   set;
}

public static DateTime nowTime {
   get {return nowTime == null ? DateTime.now() : nowTime;}
   set;
}

Change all your code references from ...Date.today()... to ...Util.todayDate...

for example,

if (myNote.createdDate < Util.nowTime) ...

or

[select id , ... from Opportunity where closeDate < : Util.todayDate]

now, your testmethods can set today's date (or now time) to any value in the past, present, or future before or after the objects are created or referenced