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

    // 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]); 
  • 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 ? 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
    Dec 14, 2017 at 11:27
  • ohh yes >= you have used sorry my bad I din't pay attention to it 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
    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
    Dec 14, 2017 at 12:38

1 Answer 1


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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.