Hide Empty Rich Text Field in Lotus Notes

The problem is very simple: I want to hide a Rich Text field (RTF) if it is empty and show it if it has something in it, in my case one or more attachments. In Lotus Notes a Rich Text Field will not evaluate to empty even if it is emtpy so using a common Hide-When forumula such as MyRTF=”” will not work and you can only use @Formulas.

The solution:

1) Create a new Numeric field, Computed for Display, Hidden, and with Value set to @ThisValue (or HideMyRTF if you are using an earlier release than ND6)  Lets call this field HideMyRTF in this example.

2) Set the Hide-When formula of your existing Rich Text Field – lets call it MyRTF in this example – simply to HideMyRTF (meaning if HideMyRTF is True then hide MyRTF)

3) Add some LotusScript using the NotesRichTextItem EmbeddedObjects property to the Forms QueryOpen event. This will check if there are attachments or text in the field:

Dim rtitem As NotesRichTextItem
Set rtitem = source.Document.GetFirstItem( "MyRTF" )
If Isempty(rtitem.EmbeddedObjects) And Trim(rtitem.Text) = "" Then
    source.Document.HideMyRTF = 1
    source.Document.HideMyRTF = 0
End If

That’s it.

It was a long time since I did any Lotus Notes development so I’m a bit rusty. I’m sure I’ve come up with this or a similar solution in the past but I could not find any when searching so I had to start from the beginning.

This was for an existing app that has been around for quite a few years so there is no way for me to control anything from the beginning. I guess I could have used an agent to set some stuff but this would most likely cause tons of replication conflicts as there are tens of thousands of documents, still being updated by users and replicated across several servers.

Done with a Lotus Notes 7.02 client (I told you it’s been a while…) but should work on older and newer releases.

How to sort a NotesDocumentCollection in Lotusscript

This Lotusscript function sorts a document collection on one or multiple fields.

I have previously used several other algorithms that use a view to sort the collection, these however have the drawback that they become very inefficient (i.e. slow) as the number of documents in the view used for sorting grows. The solution presented below does not have this problem.

It has been developed and tested in Lotus Notes 6.5.3 but should work in all ND6 (release 6) and possibly earlier Lotus Notes releases too (if you test this successfully or unsuccessfully write a comment to this post and everyone will know).

Example of use:

Dim fieldnames(0 To 2) As String
fieldnames(0) = "SKU"
fieldnames(1) = "OrderDate"
fieldnames(2) = "Client"
Set collection = SortCollection (collection, fieldnames)

Function to sort DocumentCollection:

Function SortCollection(coll As NotesDocumentCollection, fieldnames() As String) As NotesDocumentCollection

' ------------------------------------------------
' --- You may use and/or change this code freely
' --- provided you keep this message
' ---
' --- Description:
' --- Sorts and returns a NotesDocumentCollection
' --- Fieldnames parameter is an array of strings
' --- with the field names to be sorted on
' ---
' --- By Max Flodén 2005 - http://www.tjitjing.com
' ------------------------------------------------

Dim session As New NotesSession
Dim db As NotesDatabase
Dim collSorted As NotesDocumentCollection
Dim doc As NotesDocument
Dim i As Integer, n As Integer
Dim arrFieldValueLength() As Long
Dim arrSort, strSort As String
Dim viewname As String, fakesearchstring As String

viewname = "$All" 'This could be any existing view in database with first column sorted
fakesearchstring = "zzzzzzz" 'This search string must NOT match anything in view
Set db = session.CurrentDatabase

' ---
' --- 1) Build array to be sorted
' ---

'Fill array with fieldvalues and docid and get max field length
Redim arrSort(0 To coll.Count -1, 0 To Ubound(fieldnames) + 1)
Redim arrFieldValueLength(0 To Ubound(fieldnames) + 1)
For i = 0 To coll.Count - 1
Set doc = coll.GetNthDocument(i + 1)
For n = 0 To Ubound(fieldnames) + 1

If n = Ubound(fieldnames) + 1 Then
arrSort(i,n) = doc.UniversalID
arrFieldValueLength(n) = 32
arrSort(i,n) = "" & doc.GetItemValue(fieldnames(n))(0)
' Check length of field value
If Len(arrSort(i,n)) > arrFieldValueLength(n) Then
arrFieldValueLength(n) = Len(arrSort(i,n))
End If
End If

Next n
Next i

'Merge fields into list that can be used for sorting using @Sort function
For i = 0 To coll.Count - 1
If Not strSort = "" Then strSort = strSort & ":"
strSort = strSort & """"
For n = Lbound(fieldnames) To Ubound(fieldnames) + 1
strSort = strSort & Left(arrSort(i,n) & Space(arrFieldValueLength(n)), arrFieldValueLength(n))
Next n
strSort = strSort & """"
Next i

' ---
' --- 2) Sort array
' ---
arrSort = Evaluate("@Sort(" & strSort & ")")

' ---
' --- 3) Use sorted array to sort collection
' ---
Set collSorted = coll.Parent.GetView(viewname).GetAllDocumentsByKey(fakesearchstring)

For i = 0 To Ubound(arrSort)
Set doc = db.GetDocumentByUNID(Right(arrSort(i), 32))
Call collSorted.AddDocument(doc)
Next i

' ---
' --- 4) Return collection
' ---
Set SortCollection = collSorted

End Function

(This article is previously published and has been moved to this blog)

[Update: Instead of using Evaluate and @Sort in section 2 in the code you can of course use any of your favourite sort routines. Or search the Net for one, there are many out there.]