7

I have an issue with Querying a record in a unit test with a custom set createdDate using the method Test.setCreatedDate();

static testMethod void test()
{
    Account a = new Account(name='myAccount');
    insert a;
    Test.setCreatedDate(a.Id, DateTime.newInstanceGmt(2022,12,12));
    Test.startTest();

    // Success
    System.assertEquals(1, [SELECT Count() FROM Account WHERE CreatedDate >= 2011-01-01T00:00:00Z]); 

    // Fails
    System.assertEquals(1, [SELECT Count() FROM Account WHERE CreatedDate >= 2022-01-01T00:00:00Z]); 
    Test.stopTest();
}
5
  • you are using 2022-01-01 in where clause where as date you have set is 2022,12,12 could that be cause soql query not returning any data ? Commented Dec 14, 2017 at 11:25
  • No it should return it because I set >= in the where cause. 2022-12-12 >= 2022-01-1 should be true..
    – Wim
    Commented Dec 14, 2017 at 11:27
  • ohh yes >= you have used sorry my bad I din't pay attention to it Commented Dec 14, 2017 at 11:29
  • I'm not sure you're supposed to use the CreatedDate in this way. Would you mind telling something about your use case?
    – Kasper
    Commented Dec 14, 2017 at 11:38
  • I happen to run into this by accident. Just used the current date for a record, another record in the past and one in the future. Just to cover all scenarios in my unit-test. That's how I found out about this issue.
    – Wim
    Commented Dec 14, 2017 at 12:38

1 Answer 1

7

It seems that you can set a CreatedDate on a record in the future, but when querying for that record you cannot use a date in the future as condition

Although it is not likely that you will want to set a CreatedDate in the future, it is something easily forgotten when creating unit-tests.

Could we mark this as a bug in Salesforce?

1
  • I added doc feedback; and Idea (vote it up)
    – cropredy
    Commented Jan 3, 2018 at 0:59

You must log in to answer this question.

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