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.

Saturday, March 13, 2010

Domino Development Best Practice

Over the next few weeks, I will share best practice as a developer when I am developing Notes/Domino applications.

I will start the series by sharing some basic development styles which I adhere to:

- Always declare variables in a hierarchy and keep Domino object declaration different from other variables, i.e.,

Dim session as NotesSession
Dim db as NotesDatabase
Dim vwCustomer as NotesView
Dim customerDoc as NotesDocument

Dim strFirstName as string, strLastName as string
Dim varNames as Variant

As you can see - session comes first, then db, then view etc etc. Also notice that the string and other variables are kept separate from Notes objects.


- Make sure to tab and comment your code properly for your own benefit - trust me, when you come back to your own code in a year's time, without proper comments, you will have a hard time understanding why you did something in the first place.


- Try to generalize sub-routines as much as possible. I try to follow a simple rule, when a single method or function gets too big to handle, start breaking it up into smaller pieces - divide and conquer.


- Always try to simplify code in order to hide complexity and make your life simpler, i.e.:

BEFORE:

Sub AttachFile (ndDoc As NotesDocument, strFileName As String, strRichTextFieldName As String)
Dim object

Dim strFilename As String
strFilename = Dir$(filename)
If (strFilename <> "") Then
Dim rtitem As NotesRichTextItem
If ndDoc.HasItem(strRichTextFieldName) Then
Set rtitem = ndDoc.GetFirstItem(strRichTextFieldName)
Else
Set rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )
End If
Set object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)
End If
End Sub


AFTER:
Sub AttachFile (ndDoc As NotesDocument, strFileName As String, strRichTextFieldName As String)
Dim object
If FileExists(strFileName) Then
Dim rtitem As NotesRichTextItem
If ndDoc.HasItem(strRichTextFieldName) Then
Set rtitem = ndDoc.GetFirstItem(strRichTextFieldName)
Else
Set rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )
End If
Set object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)
End If
End Sub


Function FileExists(Byval filename As String) As Boolean
Dim strFilename As String
On Error Resume Next
strFilename = Dir$(filename)
FileExists = (strFilename <> "")
End Function


As you can see you will have to do some thinking in order to figure out what this line of code does in the before example:
strFilename = Dir$(filename)
If (strFilename <> "") Then
but it's obvious in the after example.

Friday, February 06, 2004

JS Technique (DHTML)

node.style.display = (node.style.display == 'block') ? 'none' : 'block';

Wednesday, December 24, 2003

Welcome

Welcome to Pipalia's blog