3

SetCreatedDate is not working, CreatedDate is null in the debug logs. What gives?

@isTest
public class PurgeOldCats_Test {

static testMethod void PurgeOldCats() {

    Date today = date.today();
    Date twoWeek = today.adddays(-14);

    List<Cat__c> testPurgeCats = new List<Cat__c>();
    for(integer i = 0; i < 200; i++) {
        Cat__c lg = new Cat__c();
        testPurgeCats.add(lg);           
    }

    insert testPurgeCats;

    for(Cat__c lg : testPurgeCats) {
        test.setCreatedDate(lg.id, twoWeek);
        System.debug(lg.CreatedDate);
    }

    update testPurgeCats;

    List<Cat__c> testSafeCats = new List<Cat__c>();
    for(integer i = 0; i < 200; i++) {
        Cat__c lg = new Cat__c();
        testSafeCats.add(lg);           
    }

    insert testSafeCats;

    test.startTest();
    PurgeOldCats curl = new PurgeOldCats(); 
    purl.query = 'SELECT Id, CreatedDate FROM Cat__c WHERE CreatedDate != LAST_N_DAYS:7';
    Id batchProcessId = Database.executeBatch(purl);
    System.debug(testPurgeCats.size());
    test.stopTest();


    System.debug(testPurgeCats.size());
    System.assert(testPurgeCats.size() == 0);



}  
}
8

3 Answers 3

5

I have verified that this does work.

@isTest class q173966 {
    @isTest static void test() {
        DateTime theDate = DateTime.now().addDays(-2);
        Account a = new Account(Name='Test');
        insert a;
        Test.setCreatedDate(a.Id, theDate);
        a = [SELECT CreatedDate FROM Account];
        System.assertEquals(theDate, a.CreatedDate);
    }
}

The problem that you have is that you forgot to query the records back after calling this method. Records in memory do not reflect the state of data in the database:

insert testPurgeCats;
for(Cat__c lg : testPurgeCats) {
    test.setCreatedDate(lg.id, twoWeek);
}
testPurgeCats = [SELECT CreatedDate FROM Cat__c];
for(Cat__c lg : testPurgeCats) {
    System.debug(lg.CreatedDate);
}
1
  • Still show null on my end. Seems weird. May 11, 2017 at 17:48
4

setCreatedDate() takes datetime as an argument.

Refer Test Class

setCreatedDate(Id recordId, Datetime createdDatetime)

So, change your code to pass argument as datetime.

DateTime twoWeek = System.now().adddays(-14);
test.setCreatedDate(lg.id, twoWeek);

Update

I have verified that this small piece of code works fine for me and date is perfectly calculating.

    DateTime twoWeek = System.now().adddays(-14);

    Account acct = new Account(Name='Account1');
    insert acct;

    test.setCreatedDate(acct.id, twoWeek);
    acct.Name = 'Account2';

    update acct;

    List<Account> lstAccount = [SELECT createdDate From Account WHERE Id=:acct.id];

    System.debug(lstAccount[0].createdDate);
1
  • Same issue, debug logs show CreatedDate is null :( May 11, 2017 at 17:10
0

This method works fine for me. You need to query the createddate record after setting the Createdate.

@isTest
 
private class SetCreatedDateTest {

    static testMethod void testSetCreatedDate() {
        Account a = new Account(name='myAccount');
        insert a;
        Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));
        Test.startTest();
        Account myAccount = [SELECT Id, Name, CreatedDate FROM Account 
                             WHERE Name ='myAccount' limit 1];
        System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12));
        Test.stopTest();
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .