You need to sign in to do that
Don't have an account?

Altering/setting system date while testing a trigger
Hi All,
I apologize if my question has been asked many times. Please point me to the right article. Thanks.
I have an opportunity trigger that accepts or rejects changes (close date) based on today's date. I need to know how to temporarily set the today's date to a particular date while testing the trigger, similar to System.RunAs() method that allows me to execute codes under a certain user's profile, except this is to "pretend" the codes are executed on a particular date?. Can someone show me the trick?
Thanks.
=Alex
I apologize if my question has been asked many times. Please point me to the right article. Thanks.
I have an opportunity trigger that accepts or rejects changes (close date) based on today's date. I need to know how to temporarily set the today's date to a particular date while testing the trigger, similar to System.RunAs() method that allows me to execute codes under a certain user's profile, except this is to "pretend" the codes are executed on a particular date?. Can someone show me the trick?
Thanks.
=Alex
I actually had a similiar issue that I just faced. From what I can tell, there is no way to set the system date, however I did find a workaround for the issue...
On the object that the trigger runs off, put a custom formula field (type = date) that simply tells you the current date (at least that is what it will do once you push to production). Make another date field (not a formula this time), and make your original date field temporarily look at this field instead of the current date. Instead of making references to the system date, reference this field.
By doing this, you can put the date you want to test for into the dummy field (I called mine Trick_The_Test__c) and because your formula field you are referencing is set to '=Trick_The_Test__c' you can test for all of your days. Once you push to production, simply change the formula back to '=today()'
Hope this helps!
- Fresh
All Answers
If the code you're testing does comparisons of System.today or System.now, there's no way to fake those values during testing. You're going to have to find a new way to implement or a new way to test. =(
This would be an interesting enhancement to the Apex testing framework, though...
In the test method itself you create an opp and update that status to closed.
Then your query will execute, hope this helps you.
Thanks for the reply, but your answer doesn't help. I know how to execute the trigger, i want to know how to simulate the execution of the trigger on a particular date; therefore, I need a way to alter the current sysdate temporarily in a test method.
I actually had a similiar issue that I just faced. From what I can tell, there is no way to set the system date, however I did find a workaround for the issue...
On the object that the trigger runs off, put a custom formula field (type = date) that simply tells you the current date (at least that is what it will do once you push to production). Make another date field (not a formula this time), and make your original date field temporarily look at this field instead of the current date. Instead of making references to the system date, reference this field.
By doing this, you can put the date you want to test for into the dummy field (I called mine Trick_The_Test__c) and because your formula field you are referencing is set to '=Trick_The_Test__c' you can test for all of your days. Once you push to production, simply change the formula back to '=today()'
Hope this helps!
- Fresh
Fresh, thanks for the suggestion.
What I did was to refactore my code into another method in an Apex class, e.g. OppTriggerUtil. The method accepts a date as an input parameter and the date will serve as the today's date. From within my trigger, I call the method and pass the System.today(), but from my test codes, I can pass any date I desire to get the test coverage as required by Salesforce.com.
Hope this helps.
Maybe with an example if you can...