Rich Text Expert Required! Baffling Problem

Hi there! I’m hoping there’s a RichText Item expert out there who can see what’s going wrong here.

I have a computed Rich Text field which is updated by the following code in the backend. Seems to work fine, but as the size of the field increases, at some point half the text (the top half) disappears. It’s as if the dup id 0, 1, and 2 disappear leaving dup id 3 behind. Can’t work out why. This is the code that is called to update the field:

Dim tempDate As New notesdatetime(cDate)

cDate = tempDate.ZoneTime

Dim comments As Variant, sess As New notessession, richStyle As NotesRichTextStyle

Set richStyle = sess.CreateRichTextStyle

Set comments = doc.GetFirstItem(“Comments”)

If comments.Type <> RICHTEXT Then Set comments = New NotesRichTextItem(doc,“Comments”)

richStyle.NotesFont = comments.GetNotesFont(“Default Sans Serif”,True)

richStyle.FontSize = 8

Call comments.AppendStyle(richStyle)

If comments.text <>“” Then Call comments.AddNewline(2)

Call comments.AppendText("Following added by " & cWho$ & " on " & cDate & “:”)

Call comments.AddNewline(1)

For dc = 1 To 160

 Call comments.AppendText("-")

Next

Call comments.AddNewline(1)

Call comments.appendtext(cComm$)

The cComm string is collected from a subform simple text field and parsed in.

Any insight would be much appreciated!

Subject: RE: Rich Text Expert Required! Baffling Problem

This line:

If comments.Type <> RICHTEXT Then Set comments = New NotesRichTextItem(doc,“Comments”)

looks suspicious to me, as it creates a new item with the same name as an existing item, and doesn’t delete the old item first. I think that might cause confusion in the back end. At best it will replace the old item, but if it does that it discards whatever is in the field to that point – which is the symptom you’re describing.

If there’s maybe a process elsewhere in your system that changes this to an item of a different type – plain text, or MIME, perhaps – then you will lose the information in it. Certainly, you should put up a messagebox or set a breakpoint in here so that you can see whether this line coincides with your data loss.

I also wonder about the context in which this code runs. If the user is editing a document, updates to a rich text field aren’t immediately available in the back end, so it may appear to have less information in it than you can see on screen. Read about the Refresh method.

What do you do after executing this code to make the data appear on screen?

Subject: RE: Rich Text Expert Required! Baffling Problem

Hiya! Thanks for your reply.

Yes, the line does like it would cause a problem - if the field was somehow reverting to standard text - 99% of the time the type is RICHTEXT so the command is ignored…I’ll try it another way.

With regards to the context - there are two ways the field is updated:

  1. Via a background agent with no user intereference

  2. The user adds, but there is never a call to uidoc.save - all saves are made to the backend. Once the update is made the document is closed and not reopened.

edit: Okay - I removed the <> RICHTEXT line and it makes no difference. One thing I noticed is that when I view the Comments field properties from the view it is broken into about 8 different COmments fields correctly, but when I go in and look at the properties and the field merges them back to 1 I can see that the top text is missing - even though the front-end display is correct…it’s when I then add the next set of text that the text disappears.

Subject: RE: Rich Text Expert Required! Baffling Problem

You might try calling the Update and Compact methods after you’re done adding text. That would at least reduce the number of items used to represent the rich text.

Can you determine whether the problem only occurs in one context?

Working with rich text you got from a NotesUIDocument is a little bit tricky. Using a back-end save on a document that you have open on screen, is rarely a good idea. You haven’t shown enough code, nor described the process in enough detail, for me to draw any conclusions about whether you’re doing it right.

If the above does not solve your problem, we need more information about what you’re doing. If you’re not sure what information to supply, the C R I S P Y document might help you.

  • Andre Guirard, IBM/Lotus Development

Useful blog: Best Practice Makes Perfect

Subject: RE: Rich Text Expert Required! Baffling Problem

I tried compact and update and they didn’t help either. I am sure the problem occurs both from the user update and the agent update.

The field being updated is a text log - comments are added in 3 situations:

  1. If user clicks Add Comments button a dialog appears with a text field - user enters plain text and it’s appended.

  2. User front-end activities will also result in system-generated text being added prior to the save call

  3. Back-end agent adds further system-generated text

Below is the code for the Add Comments button which the user can click at any time…

Set ws = New notesuiworkspace

Set sess = New notessession

Set db = sess.currentdatabase

Set profile = db.getprofiledocument("Database Profile")

Set uidoc = ws.currentdocument

Set doc = uidoc.document

Set subform = New notesdocument(db)



'Get time from server; if it fails take local machine time

serverTime = Evaluate({@Now([ServerTime]:[LocalTimeOnError];@GetProfileField("Database Profile";"prfServer"))})

Dim sTime As New notesdatetime(serverTime(0)) 'set datetime for date fields



Dim user As New notesname(sess.username)



title$ = "Add Miscellaneous Comments"



'Prompt user to enter comments

subform.Type = "Comments" 'indicates which prompt to display

If ws.Dialogbox ("Comments", True, True, False, False, False, False, title$, subform) = False Then Exit Sub



If doc.Category(0) = "Unknown Payment" Then bypassQMC = True ----Global to bypass the QueryModeChange



If uidoc.editmode = False Then uidoc.editmode = True ----I've tried without this line, too.



'Add comments, if any

comments$ = "[Miscellaneous Comments]: " & subform.Comments(0)

Call UpdateComments(user.abbreviated,comments$,serverTime(0),doc) ----The routine already posted



call doc.Save(true,false)



Call uidoc.close



Msgbox "Your comments have been successfully added to the Transaction.",0+64, title$	

The other 2 situations simply set a comments$ string and calls the UpdateComments routine before saving it with a call doc.save(true,false)

Subject: RE: Rich Text Expert Required! Baffling Problem

Nothing leaps out at me. You’re going to have to debug it further. In your UpdateComments routine, put up a messagebox showing the current text in the rich text field. Determine in what circumstance you’re losing the data. As I understand you, the data that goes missing is from the top of the field, while some intermediate data is undisturbed. This seems odd to me. Look at the sequence numbers in the rich text items – are they unique and sequential (long shot, but this is a weird problem you’re having).

Subject: *void - double submit… /eom

Subject: RE: Rich Text Expert Required! Baffling Problem

hm, a bit surprised that backend agent has the same problem. If you use computed rich text and u update it while the document is open you are bound to have problems.

Try - make the field “editable”. If it solves the problem for the UI updates, then keep the backend field that never updates “comment” and make the frontend fied “commentUI” computed from “comment”. Then all the updates go to the field you never show directly in UI.

if this does not help then dunno. +the mystery of agent acting the same… try to nuke the form or use another - fresh one for the test to see if there is something corrupt…

Subject: RE: Rich Text Expert Required! Baffling Problem

Okay! You’re right - the back-end update does not have the same affect, however it does seem equally responsible for messing the field up.

I ran a back-end agent about twenty times to add an enormous amount of text each time. The text DID NOT disappear from the field, however, when checking the Comments field’s properties from inside the document I could see that the values did not represent those I could see in the document. They represented what would appear once I added via the front-end. Despite this, the back-end updates continued successfully.

So, it would appear to be a front-end problem. I suspect that if the field was editable I’d be okay, but I need this field to be computed because it is an audit trail that shouldn’t be badgered with. (EDIT: Would putting it into a permanently-frozen computed section solve that problem!?)

I’m a little confused about your suggestion for two fields - are you saying I should leave “comments” and update as usual but have this redirected to “commentsUI” which is computed with the “comments” values?

Thanks!!!

EDIT2 - Making the field editable and placing it in a computed controlled section ‘appears’ to have done the trick - for the user updates anyway…need to test back-end, too.

But should I really have to do this? What’s the problem with updating a computed RTI obtained from the UI?

Subject: RE: Rich Text Expert Required! Baffling Problem

you might have used some other approach - i was probably unclear about my advise… but the bottom line is - for whatever reason, computed rich text item retrieved while UI doc is open contains different stuff than when it is retrieved from the backend.

So modification of this field results in adding of your new content to this “screwed up” item and will result in different problems.

Subject: RE: Rich Text Expert Required! Baffling Problem

Okay, it looks like it’s working okay by leaving the field editable but frozen in a section. Thanks ever so much to both of you for your help - a great insight!

It’s a pity Lotus don’t put these insights into their Help documentation for RTIs - it would save a lot of hassle…or do they!?