Posted on Feb 03, 2017

Just a short post about an old lesson that I was reminded of today.

I've done a few projects for some financial institutions that include date sensitive operations and calculations, things that have to be aware of the current date and will change values depending on what the date is, or processes that need to execute in a certain order and need to be completed all within the same day, etc.

One of the key things we realized early on is that we would need to build these calculations (and to some extent the processes) so that:

  • The calculations had the flexibility to be executed "out of time" ie. you had the ability to simulate what the current date/date time was so you could get the results out of the calculations that you wanted.
  • The system had built-in time-senstive reminders to prompt users to take certain actions and not forget.

In the former, this was important for a few reasons:

  • Salesforce Test classes: Test classes can be run at any time, so you'll either have to make sure your test data and tests can always just use the current datetime, or you need to control datetime so that your tests always run on a static, expected date.  In my scenarios, the data was too complex and required too much domain knowledge to be able to develop a non-date-sensitive set of data, so we made the date static.
  • Historical data loading: If you have any amount of historical data from that past that needs to be loaded into the system, then you need to be able to control the date so that all your calculations line up and product the expected results with the data that's being loaded.
  • Fixing mistakes: This is a key one. The client will miss something.  They won't approve something on time, they'll miss loading some data, they'll load the wrong data, etc.  So sometimes you'll have to delete existing data and back up the system to another point in time so that the missing actions can be performed.  I cannot count the number of times I was thankful that we built "time travel" into the system!

To facilitate the shifting of time, we created a constants class which had a static date variable that returned a date or date time.  The function would look at a custom setting for a checkbox and a date field, and if the checkbox was ticked, return whatever value was in the date field.  Otherwise, either return the current date, or if we were currently running a test, check to see if the static variable already had a value that was set by the test class and return that instead.

For reminders, it should probably be obvious that no matter how much a client says it's important that X get done by Y time, some one's deadline is not always at the top of another's todo list.  Reminders to go approve a record or look up some data and enter it into the system can be annoying when the client is on top of their game, but some days they get stuck in meetings or they're not paying attention, etc.  Reminders will help make sure that the work gets done when it needs to get done.

A lot of this is probably obvious after you read it, but if not, hopefully this will help you avoid problems in the future!