10

I have a class that processes some data returned by a query. The query is based on the createddate column of the object. In this case, the query specifically retrieves data created 3 days before today.

So as an example, the query would have the

Select Id, Name, Amount__c FROM MyCustomObject__C WHERE CreatedDate = TODAY - 3 as the condition to fetch records.

In my test class any records I insert in the MyCustomeOBject__c would always have the CreatedDate = TODAY

So how can I make test data that was created 3 days ago?

18

You can use following:

Test.setCreatedDate(recordId, createdDatetime);

Sets CreatedDate for a test-context sObject.

Datetime testingday = Datetime.now().addDays(-3);
Test.setCreatedDate(MyCustomObject__c.Id, testingday); 
  • Good answer. I am stuck in another part of my test class. I believe this will solve the issue. Thx. – abhi Jul 7 '16 at 18:57

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.