Monday, October 8, 2007

One Liners in LotusScript

Update, 15.10.07
Discovered that Run/RunOnServer also is "chainable"

Call s.CurrentDatabase.GetAgent( "TestWQO1" ).Run()
Call s.CurrentDatabase.GetAgent( "TestWQO1" ).RunOnServer()

Call s.GetDatabase( "server", "path" ).GetAgent( "TestWQO1" ).Run()
Call s.GetDatabase( "server", "path" ).GetAgent( "TestWQO1" ).RunOnServer()


Actually two lines, since you have to have a NotesSession-variable.
Dim s As New NotesSession

Get document:
s.CurrentDatabase.GetView( "viewname" )._
GetDocumentByKey( "key" )

You'd normally want to assign this to a variable -> more lines..

Based on the above, I thought you could do:
s.GetDatabase( "server", "path" ).GetView( "viewname" )._
GetDocumentByKey( "key" )

But it seems that a NotesDocument needs it parent database in memory. CurrentDatabase is a property of NotesSession.

Empty a view:
s.CurrentDatabase.GetView( "viewname" )._
AllEntries.RemoveAll( True )

s.GetDatabase( "server", "path" ).GetView( "viewname" )._
AllEntries.RemoveAll( True )

Empty a db:
s.CurrentDatabase.AllDocuments.RemoveAll( True )
s.GetDatabase( "server", "path" ).RemoveAll( True )

Delete documents based on form/etc:
s.CurrentDatabase.Search( |Form="SomeForm"| )._
RemoveAll( True )

s.GetDatabase( "server", "path" )._
Search( |Form="SomeForm"| ).RemoveAll( True )

AllDocuments and Search returns NotesDocumentCollection, so you could also use StampAll, etc.


Charles said...

If the collection returns empty, does it not fail?

William Beh said...

I don't see much point to create one liner like those. In most cases you will always need to reuse the db / view objects. Thus it is always better to declare separate variables for them.

Tommy Valand said...

@Charles: It most certainly will :)

@William: There are several occasions where I've used a DB/View-variable to get to a document or a collection.

With a document, it has mostly been getting a config/keyword-document.

Collections for doing cleanups.

Most recently I used the .Search one-liner to clean out one of my earlier applications, which I discovered created a document for each search. There were 2000+ search document.

I fixed the form, made an agent ...Search..RemoveAll. Ran the agent onserver, using the AgentRunOnServer toolbar button. Done in 60 seconds (well, almost).

One-liners are in most circumstances bad practice, but I believe they have their place, at least in my code. :)

Nate said...

William, chaining methods is an honored tradition and can dramatically improve the readability of much of your code. I've been known to use lines like...

If Me.p_refDoc.document.universalID <> Me.cache.workspace.refWorkspace.currentDocument.document.universalID Then...

...frequently. (Yes, I did just go copy that from a library.)

cue said...

how can you get a listing of the available views for GetView and available items for GetItemValue ??