Test.setCreatedDate fails on note record created in test context

I’m trying to get a number of records inserted into the database sometime in the past, to allow for a cleanup batch to have proper test coverage. I can use Test.setCreatedDate on the account I’m attaching a number of notes too, but I can’t use it on the notes themselves.

public static void Test_SetCreatedDate() {
    Note n = new Note();
    n.Title = 'Naming Criteria';
    n.Body = 'Testing Note.';
    Account a = Generate.CreateAccount();
    insert a;
    n.ParentId = a.Id;
    insert n; 
    Test.setCreatedDate(a.Id, DateTime.now().addMonths(-6));
    Test.setCreatedDate(n.Id, DateTime.now().addMonths(-6));
    a = [SELECT Id, CreatedDate FROM Account WHERE Id = :a.Id];
    n = [SELECT Id, CreatedDate FROM Note WHERE Id = :n.Id];
    System.assertEquals(DateTime.now().addMonths(-6), a.CreatedDate);
    System.assertEquals(DateTime.now().addMonths(-6), n.CreatedDate);

This code is failing with:

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

As far as I can tell, this is a test method, with everything (including the Generate class), located in a test context.

Heres the Generate.CreateAccount function, requested in the comments. All it does is act as a scaffold to provide data that fulfills the requirements for record creation, without inserting a record.

private final static User user = [SELECT Id FROM User WHERE IsActive = true LIMIT 1];

private static Account CreateAccount() {
    Account account = new Account();
    account.Name = 'Test';
    account.OwnerId = user.Id;
    account.Description = 'Used for testing.';
    account.NumberOfEmployees = 1;

    // other fields 
    return account;


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.

public class Test_SettingCreatedDateOnNote {

    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));


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



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.

Source : Link , Question Author : battery.cord , Answer Author : Daniel Ballinger

Leave a Comment