+ Start a Discussion
Sean Healy 33Sean Healy 33 

[Bug] Test.setCreatedDate fails on Note record created in test context

Was trying to file a case for support to make sure this is a bug, but was pushed here instead. 

Below is a simple snippet, that can be run in a spring'16 developer org, which reproduces a System.NoDataFoundException. The test class was designed to create a number of notes, set the created date in the past, and then run a batch apex to remove notes created past a certain date. When trying to run the class, the exception is generated. The account created in the snippet does not generate the same exception. 
@isTest
public class NoteCleanUp_Test {
    
    @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 above code will generate the exception:
System.NoDataFoundException: The sObject with the ID 00261000003I5rmAAC isn’t part of this transaction. Provide the ID of an sObject that this test method created.

With a stack trace of:
Class.System.Test.setCreatedDate: line 81, column 1
Class.NoteCleanUp_Test.CleanUp_Test: line 25, column 1

I've also posted this on the salesforce stackexchange network, which you can view here: http://salesforce.stackexchange.com/questions/117473

I'd really like either someone to confirm that yes, this is in fact a bug, or for someone to point out a error in the above code to allow me to write a test class without using a workaround (such as those specified on the linked stackexchange question).
Pankaj_GanwaniPankaj_Ganwani
Hi,

I just run the below mentioned test class in my dev org and it seems to be working fine. Make sure the API version of the class should be set to 36.0.
 
@isTest
private class AccTestClass
{

    private static testMethod void sampleTest() 
    {
        Account objAcc = new Account(Name='Test1234',ShippingPostalCode = '591237');
        insert objAcc;
        
        Test.setCreatedDate(objAcc.Id, DateTime.now().addMonths(-6));

    }

}

 
Sean Healy 33Sean Healy 33
@Pankaj_Ganwani, the API version of the test class is set to 36.0. Additionally, the account object's created date can be set back without issue- the problem arises when trying to set back a note's created date. Can you get the exact snippet above running in a developer org?
Daniel BallingerDaniel Ballinger
Duplicating answer from SFSE (http://salesforce.stackexchange.com/a/119469/102):

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 [in SFSE].

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.