Friday, April 09, 2010

Lotus Notes Validation

This week we will look at how we can handle validation within Lotus Notes using LotusScript.

Here is a class I have written which defines some basic validation routines:

' Copyright (c) Pipalia IT Consultants 2010
' Please keep the above copyright line, if you decide to use this class in your code
Class ValidationError
strErrorMsg() As String ' Array to store messages
counter As Integer
' Initiate the queue
Sub New()
counter = 0
End Sub
' Add error to an array
Sub AddError(strMsg As String)
Redim Preserve strErrorMsg(counter)
strErrorMsg(counter) = strMsg
counter = counter + 1
End Sub
' get all error messages
Function GetError() As String
Dim strError As String
Forall msg In strErrorMsg
If strError = "" Then
strError = msg
Else
strError = strError + Chr(13) + msg
End If
End Forall
GetError = strError
End Function
' determine whether an error has been raised
Function IsError() As Boolean
If counter = 0 Then
IsError = False
Else
IsError = True
End If
End Function
' check if field value is empty
Sub EmptyFieldError(strFieldVal As String, strMsg As String)
If Trim(strFieldVal) = "" Then
AddError(strMsg)
End If
End Sub
' check if given value (converted to single data-type) is positive
Sub PositiveValueCheck(strFieldVal As String, strMsg As String)
Dim sFieldVal As Single
If Trim(strFieldVal) <> "" Then
If Isnumeric(strFieldVal) Then
sFieldVal = Csng(strFieldVal)
If sFieldVal <>
AddError(strMsg)
End If
Else
AddError(strMsg)
End If
End If
End Sub
Sub IsGreaterThanZero(strFieldVal As String, strMsg As String)
Dim sFieldVal As Single
If Trim(strFieldVal) <> "" Then
If Isnumeric(strFieldVal) Then
sFieldVal = Csng(strFieldVal)
If sFieldVal <= 0 Then
AddError(strMsg)
End If
Else
AddError(strMsg)
End If
End If
End Sub
' Check if given card number is a numeric value
Sub IsCardNumber(strFieldVal As String, strMsg As String)
Dim strCharacter As String
For x = 1 To Len(strFieldVal)
strCharacter = Mid(strFieldVal, x, 1)
If (Asc(strCharacter) > 47 And Asc(strCharacter) <>
' Numeric value
Else
AddError(strMsg)
Exit For
End If
Next
End Sub
' Raise an error if field value length is higher than maximum
Sub CheckValueLength(strFieldVal As String, intSize As Integer, strMsg As String)
If Len(strFieldVal) > intSize Then
AddError(strMsg)
End If
End Sub
' Used to add any generic error message to the list
Sub GeneralError(strMsg As String)
AddError(strMsg)
End Sub
End Class

We will look at what this code does and how to use it during validation in the next session.

Friday, April 02, 2010

Error handling in Notes

If we were using .NET or Java, we could easily handle errors using Try and Catch, but that's not the case in Notes, so it's important to know where exactly the error occurred - which module, what line and where it was called from. Knowing this information helps us resolve the problem in a quick and efficient manner. Let's now look at how we can achieve something similar in Notes:

Msgbox Lsi_info(2) & " called by " & Lsi_info(12) & "(" & Lsi_info(430) & "): " & Error$ & " - Line: " & Cstr(Erl), 16, "Title"

I read an interesting article on Dominopower (http://www.dominopower.com/issues/issue200606/00001780001.html), which lead me to update my error handling code.

So what does it do you may ask:

1) Lsi_info(2) - returns the current module/sub/function name
2) Lsi_info(12) - returns the parent module that called this routine or function.
3) Lsi_info(430) - returns the line number in the calling module that called this module.


It's always a good idea to log all your errors in a database as well as displaying a generic error message to the user. This is where Julian Robichaux's OpenLog (http://www.openntf.org/Projects/pmt.nsf/ProjectLookup/OpenLog) template comes in handy.