Monday, 26 May 2008

Lotusscript NotesDateTrouble

The other week marketing requested a function (agent) to send a follow up email x days (follow up days) after recieving a document (an order in this case).
Not a difficult function to build and NotesDateTime gives us the properties and methods to determine if a document is more than x days old.
So I build the agent that compares the created date of a received document to the date of today, both transferred to a NotesDateTime class. For the today value I used the "today" function. The agent will run once a day at appr. 14:00 hrs.

Sample of the code (simplified for ease of reading)
Dim dtToday As NotesDateTime
Dim dtCompare As NotesDateTime
Set dtToday = New NotesDateTime ( Today )
Set dtCompare = New NotesDateTime ( Now )
If dtCompare.LSLocalTime <= dtToday.LSLocalTime Then
...Print 'compare date is less-equal today, send follow up'
End If


All seems to works fine if you work with documents that are older than a day (x > 0), but when we wanted to test if it worked when x = 0, no follow up emails were send.
In the sample code above I replaced the created date by "Now" but the problem is in the "Today" function.
Only when inspecting the assigned values in the debugger, you can see why it doesnot work. The "Today" function doesnot set any time value in the NotesDateTime class (see the screenshot), but in the comparison it seems to assume a time of 00:00:00 (the start of the day) and thus the follow up is not send.

To solve the problem setting dtToday to "Now" is an option, but will only partially solve the problem as it will ignore documents received after the time set by "Now" until the next day.
So instead I decided to set dtToday to the end of the day by combining the "Today" function with a fixed time string of "23:59:59".
In the (simplified) code below you can see how it works.



Set dtToday = New NotesDateTime ( Today & " 23:59:59" )
Set dtCompare = New NotesDateTime ( Now )
If dtCompare.LSLocalTime <= dtToday.LSLocalTime Then
...Print 'compare date is less-equal today, send follow up'
End If


The message I want to give here is when using NotesDateTime comparisons, carefully look at the values displayed in the debugger to see if it works the way you think it should work.
Initially I did not do this and was puzzled as to why the comparison did not work until I started looking at the assigned values of the classes in the debugger.


1 comment:

Mike VandeVelde said...

I've just gone through some of the same issues - try it with different time zones in the mix! If the document was created at x time in time zone a, should it be sent at y time in time zone b when the server thinks it's z time in time zone c? Now is definitely better than Today in that kind of circumstance.

One other tip: check out the undocumented Merge method of NotesDateTime. It can be very handy. Only found it in the mail template, and it is mentioned here:
http://opendom.blogspot.com/2006/11/undocumented-dom-lotusscript-inventory.html

Hope that helps.

Mike VandeVelde
http://www.teamspace.ca/