function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mitchell McConnell 2Mitchell McConnell 2 

How to fake date for testing

Hello all,

I have been away from Salesforce development for a few years, and am just getting back to it.   I have an app I have created in my developer sandbox, and I am creating some REST code with Python for testing. 

I have some Apex code that causes things to happen based on the date.    So I might have something that should happen one month from today.   How can I cause my instance to run with a date that is in the future, so I can test my code?

Thanks,

Mitch
Sachin HoodaSachin Hooda
Hi Mitchell,
Please specify for what field you wanted to fake the date.
If you want to fake the created date of a record. You can always achieve it with setCreatedDate of Test Class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm) method.
Or if you want to set a date to a custom field you can achieve that like
CustomObj__c obj =  new CustomObj__c();
obj.CustomDateField__ c = Date.newInstace(2020,06,16);
I hope it helped you. In case I didn't get what you actually needed. Please post more info.
Mitchell McConnell 2Mitchell McConnell 2

Sachin,
Thanks for reminding me about the Test classes.   Since this is a private sandbox, I have not (yet) created my test classes. 

When I do, I will use your suggestion.

Thanks,

Mitchell