Help with agent to modify a field when previewed

I am trying to get an agent that runs on PostOpen that modifies a ViewedBy field with the user name. The trouble I am having is isolating the modify to only the document that is open, not all documents. I don’t see a setting for that in the runtime target options.

Help!

Subject: Help with agent to modify a field when previewed

Not sure what your code looks like, but maybe this will help.

Opens the current or a specified document in a mode you specify. The current document may be either:

The document that’s currently open.

The document that’s currently selected in a view or folder.

Defined in

NotesUIWorkspace

Syntax

Set notesUIDocument = notesUIWorkspace.EditDocument( [editMode [, notesDocument [, notesDocumentReadOnly]]] )

Parameters

editMode

Boolean. Optional. Specify True to open the document in edit mode. Specify False to open the document in read mode or put the document in the given edit mode. If you omit this parameter, the document is opened in edit mode.

notesDocument

Opens the specified document instead of the currently selected document.

notesDocumentReadOnly

Boolean. If true, sets notesDocument to read-only mode, which prevents the user from later switching to read-write mode. This parameter does not open the document in read mode; you must seteditMode to False. This parameter has no effect if you do not specify notesDocument.

Return value

notesUIDocument

The document that’s just been opened.

Examples: EditDocument method

  1. This view action script opens the document that’s currently highlighted in the view, in edit mode.

Sub Click(Source As Button)

workspace As New NotesUIWorkspace

Call workspace.EditDocument( True )

End Sub

  1. This view action script opens the document that’s currently highlighted in the view, in edit mode, and moves the insertion point to the Body field.

Sub Click(Source As Button)

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Set uidoc = workspace.EditDocument( True )

Call uidoc.GotoField( “Body” )

End Sub

Subject: RE: Help with agent to modify a field when previewed

Michael, thank you for your response. While waiting, I continued to get it working with my own knowledge. I was able to get this to work. BUT…it requires you to have editor right or above. I have a feeling the same would be true with your script. Any idea how to get around that?

SELECT @All;

ViewedBy := @Subset(ViewedBy; 50);

@If(@IsNotMember(“[Admin]”;@UserRoles);

FIELD ViewedBy:= ViewedBy + @Char(13) + @Name([CN];@UserName) + " - " + @Text(@Now);“”)

Subject: RE: Help with agent to modify a field when previewed

If you need to do something that the user doesn’t have access to do, you can generally do so with an agent called with NotesAgent.RunOnServer method – this agent runs on the server with the signer’s authority. However, this only works if the database is on a server – not in a user’s local replica. They can make a change to the document in their local replica, but this change will not replicate to the server unless the server recognizes the user’s authority to make the change.

You might want to reconsider your design approach. There are some drawbacks to doing this the way you want to do it.

To begin with, you will need to deal with save conflicts, since it’s very likely two users will start to read some document at the same time.

If you have multiple replicas of the application, replication conflicts are guaranteed.

Performance may suffer because you’re modifying documents unnecessarily, causing lots of churn in the view indexes and making the database use space inefficiently.

You may have to use a non-summary field for your “read by” list. Depending how many users of this database there are, the list of who has read the document may push it over the limit for summary information in a single field or in the document as a whole.

You’re overwriting system information that would tell you when the document was last edited – you can see the last however many edit times you specify in the $Revisions field, but you can’t distinguish “real” edits.

Just because the user had the document open once, is no guarantee that they’ve read it. Especially if you’re tracking opening in the preview pane, they might have arrowed past it and it was on their screen for one second or less. If this is important information that you want to make sure has been read and understood, it’s better to give them an action button they can click to say that they’ve read the document.

If your need to track readership is not that strict that the user needs to manually affirm understanding, then the system already tracks who has read each document, and you can use the Notes API to retrieve the list of unread documents for each user. Couldn’t you use this to tell whether documents have been read? That way, users could manually mark as unread any document they didn’t actually take time to read after they opened it.

Alternately, if you could keep track of this information in another database, perhaps even a non-Notes database, you could use @MailSend to have users automatically send in “read slips” to a mail-in database that tracks the reader information for each user, discarding any duplicates. It could consolidate the information in a central location which whoever needed to, could read (e.g. you could use @DbLookup to pull up reader list information for the document you’re looking at, on demand).

Subject: RE: Help with agent to modify a field when previewed

Andre, thank you very much for the suggestions. I think the easiest method is the one you mentioned near the end. I am going to trigger a @mailsend. There are not a LOT of users, maybe 100, and they are all hitting one db on a server. I know for sure there would be conflicts with multiple people in the document. @Mailsend sounds like the safest, and easiest approach.

Thank you