Subject: RE: Lotus Script agent to export an email to a text file
Well, let’s start with this little tidbit: “Agent ran” does not mean “Agent ran without errors”, so you can’t assume that you have permission to create (or even read) a file on the server.
Even in VB (from which LotusScript is largely derived), one would generally not create production code without error checking. Macros for personal use are one thing, but if you are writing code for others to use you really need to address potential failures. Some you will be able to anticipate (like invalid data) and write around, some will cause your code to fail with no way to work around the problem – but you have to be able to tell the difference.
(And yes, it is true that most of the code posted here does not contain error checking. That’s not because we don’t use it, but because it often means as much or more code as the routine we are trying to describe. It is assumed that suggestions posted in response to questions will be adapted in a safe manner.)
The general form of checking for major errors (those that you wouldn’t be able to handle locally with an If…Then “guard statement”) looks like this:
Sub Initialize
On Error Goto errorHandler
.
. 'Active code goes here
.
getOut:
Exit Sub
errorHandler:
.
.'do something with the error
.
Resume getOut
End Sub
You can get pretty specific about the way you handle errors. (See Designer Help for the On Error statement.) The example above assumes a fatal error, one that leaves you no real option but to make a note of the problem and bail out. And the best way to make a note of the problem is to use the NotesLog class. That can log to a database (which must be based on the alog4.ntf template) or to the built-in agent log (right-click on the agent entry in the view of agents in Designer and click on Log to view). You would add this to your code to log to the built-in agent log:
Dim errorLog As NotesLog
Set errorLog = New NotesLog(db.Name + " - " + “AgentNameHere”)
Call errorLog.OpenAgentLog
That sets up the agent log. Then, in the errorhandler section of the code, you’d do something like this:
.
.
.
errorHandler:
Call errorLog.LogError(Err, Error & " at line " & Erl)
Resume getOut
End Sub
Again, see the Designer Help entries on the NotesLog class, the Err, Error and Erl functions for more info. LotusScript isn’t difficult, but don’t treat it as a toy either. If you are coding applications for other people to use, take it seriously.