Archives for the ‘LotusScript’ Category

21 Sep 2012
3

LotusScript Tip: Set Readers and Authors Fields in new Documents

Hi Everyone. I’ve been using this forever but I noticed recently that it’s not really common knowledge.

Let’s say you have a Form Design and in it you have a Readers and Authors field. Now let’s also say that you create NotesDocuments for this Form Design in the back-end. The question is: How do you populate a field in LotusScript and turn that field into a READERS or AUTHORS if required.

Many use a famous trick called “ComputeWithForm” which is a method that belongs to the NotesDocument Object. In simple terms, a ComputeWithForm will evaluate the entire NotesDocument against the Form Design it’s associated with….i.e. If you have a Form with 50 Computed Fields that contain various @Formulas, a ComputeWithForm will evaluate each of those Formulas for your NotesDocument Object.

e.g.

‘ VARIABLES
Dim ss as New NotesSession
Dim db as NotesDatabase
Dim doc as NotesDocument
 
‘ CODE
Set db = ss.CurrentDatabase
Set doc = db.CreateDocument
 
Call doc.ReplaceItemValue( “Form”, “FormAliasName” )
Call doc.ComputeWithForm( False )
Call doc.Save( True, True )

 

While this can do the job and save you a lot of coding, a ComputeWithForm can make your code terribly slow depending on how many fields and Computed Values are in the Form and how many documents you plan on computing. I’ve seen a ComputeWithForm take 2 seconds to evaluate 1 NotesDocument. In that time your code could’ve achieved the same goal and a lot more if you wrote your code to not use it. Anyways, moving on.

 

If you didn’t use ComputeWithForm, you would land up having an issue with Readers and Author Fields in your NotesDocument:

e.g.

‘ VARIABLES
Dim ss as New NotesSession
Dim db as NotesDatabase
Dim doc as NotesDocument
 
‘ CODE
Set db = ss.CurrentDatabase
Set doc = db.CreateDocument
 
Call doc.ReplaceItemValue( “Form”, “FormAliasName” )
Call doc.ReplaceItemValue( “CurrentReaders”, “John Jardin/Ukuvuma” )
Call doc.ReplaceItemValue( “CurrentAuthors”, “John Jardin/Ukuvuma” )
Call doc.Save( True, True )

 

If you had to open the DocumentProperties for this Saved NotesDocument and in the Field Properties Tab click on CurrentReaders, you will see it’s a Normal Text Field. This can cause massive issues from an Access point of view.

To solve this problem, we turn to our ever faithful friend…….NotesItem. Using the NotesItem Object, you can set the Field Type in your code. See below Examples 1 and 2:

e.g.

‘ VARIABLES
Dim ss as New NotesSession
Dim db as NotesDatabase
Dim doc as NotesDocument
Dim item as NotesItem
 
‘ CODE
Set db = ss.CurrentDatabase
Set doc = db.CreateDocument
 
Call doc.ReplaceItemValue( “Form”, “FormAliasName” )
 
Example 1
 
Call doc.ReplaceItemValue( “CurrentReaders”, “John Jardin/Ukuvuma” )
Call doc.ReplaceItemValue( “CurrentAuthors”, “John Jardin/Ukuvuma” )
Call doc.ReplaceItemValue( “CreatedBy”, “John Jardin/Ukuvuma” )
 
Set item = doc.GetFirstItem(“CurrentReaders”)
item.Isreaders = True
 
Set item = doc.GetFirstItem(“CurrentAuthors”)
item.Isauthors = True
 
Set item = doc.GetFirstItem(“CreatedBy”)
item.Isnames = True
 
Example 2 – (Preferred Option)
 
Set item = New NotesItem( doc, “CurrentReaders”, “John Jardin/Ukuvuma”, READERS )
Set item = New NotesItem( doc, “CurrentAuthors”, “John Jardin/Ukuvuma”, AUTHORS )
Set item = New NotesItem( doc, “CreatedBy”, “John Jardin/Ukuvuma”, NAMES )
 

Also, so you know, SSJS for XPages also has a NotesItem Object that you can use similar to what we’ve done here. I’m not sure about Example 2 though.

Until next time, happy coding

John.

24 Oct 2011
3

LotusScript Tip: Return Profile Document from another Database

In all my years of Notes Development, this is my first time I had to return a Profile document to the User, where the profile document existed in another database.

The good news is that this is easier than 123:

1. You will use LotusScript

2. Use the GetProfileDocument method in the NotesDatabase Object, and return it to your NotesDocument Object.

3. Use the EditDocument method in the NotesUIWorkspace Object to return the ProfileDocument to the Current User.

Example:

Sub Click(Source As Button)
   ‘ VARIABLES
Dim ss As New NotesSession
Dim ws As New NotesUIWorkspace
Dim condb As NotesDatabase
Dim myprofile As NotesDocument

    ‘ CODE
 Set condb = ss.GetDatabase( “ServerName”, “FilePathAndName” )

    If condb.IsOpen Then
   Set myprofile = condb.GetProfileDocument( “ApplicationPreferences” )
Call ws.EditDocument( True, myprofile )

 Else
Messagebox “Cannot find Database. Please contact IT Support”, 0 + 48, “Operation Failed”
  End

   End If

End Sub

Enjoy :)

24 Oct 2011
2

LotusScript Tip: Use StampAll+Multi to avoid unnecessary Looping

Hi All. I modify existing code daily and notice poor methods that developers sometimes use to get things done.

Example:

In LotusScript, if you have a Document Collection, and for each of these documents, you want to set the field Status with a value of “Submitted”,  the following seems to be the common approach:

Set NotesDocument = NotesDocumentCollection.GetFirstDocument

While Not( NotesDocument is nothing )
Call NotesDocument.ReplaceItemValue( “Status”, “Submitted” )

Call NotesDocument.Save( True, True )

Set NotesDocument = NotesDocumentCollection.GetNextDocument( NotesDocument )

Wend

Some might use For Loops, or Do While Loops instead of While-Wend, which doesn’t matter, because the good news is that there is a simple method that can replace the above code:

Call NotesDocumentCollection.StampAll( “Status”, “Submitted” )

Update on 13th September 2011

Hi Everyone. Thanks to “Lars Berntrop-Bos” for pointing out the following:

In version 8.5, IBM introduced another Method called “StampAllMulti” which works on both a NotesDocumentCollection and a NotesViewEntryCollection.

This function lets you use a NotesDocument Object as a Data Template for your Collection. All data in fields created in the NotesDocument Data Template will be sent through to the documents in the NotesDocumentCollection.

It will look something like this:

Set NotesDocument = NotesDatabase.CreateDocument

With NotesDocument

.FieldOne = “Value One”

.FieldTwo = “Value Two”

End With

Call NotesDocumentCollection.StampAllMulti( NotesDocument )

Just 2 NOTES:

1. If the fields don’t exist in the Docs in the DocumentCollection, they will be created.

2. If the Data Types are not the same, the Data Type in your NotesDocument Data Template will take preference, and will overwrite the Data Type fields in the DocumentCollection.

That’s It :)
24 Oct 2011
0

LotusScript Tip: Stop Notes View from reloading in LSScript

   Hi Everyone.
In my previous post, I explained how you could stop a Front-End Document from Reloading if you’re working with a Back-End NotesDocument. In this post, there is a similar issue which occurs with NotesViews.

Let’s say you have a LotusScript Agent which currently loops through documents in a NotesView, and during this loop, new documents are created by users, which get automatically populated into this view whilst you are using it. This annoys me, because either I get errors when working with Doc Collections, or my indexes could possibly get screwed up. The reason documents will automatically get added or removed from this view, is because this View’s Properties could be set to Refresh its Index Automatically. I usually prefer this when creating NotesViews, as it means that the data in the views is always the latest and Real Time.

So, the question is……how can we then stop the Refreshing of the View when working with it in a LotusScript Agent? Simple. When you declare and set the NotesView Object…there is a Property called “AutoUpdate”, which is default to True. Simply set this property to “False” before working with the view. See below Example:

Sub Initialize

    ‘ VARIABLES
    Dim ss As New NotesSession
    Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument

    ‘ CODE
Set db = ss.CurrentDatabase
Set view = db.GetView( “ViewName” )

    view.AutoUpdate = False
Set doc = view.GetFirstDocument

    While Not( doc Is Nothing )
Set doc = view.GetNextDocument( doc )

Wend

End Sub

24 Oct 2011
0

LotusScript Tip: Stop UI doc from trying to Save in LSScript

Hi All.

I’ve seen a few scenarios where a person, using LotusScript, uses the NotesUIDocument Object to return the Back-end Document. From there, the person will work with the back-end document and will want to close off the NotesUIDocument, but when performing the call NotesUIDocument.Close, the Systems asks if you want to save the NotesUIDocument.Now, in some cases this is correct, especially if you’ve not saved the NotesUIDocument as yet, but in other cases where you have already saved the NotesUIDocument , it will still prompt you when trying to close the UIDoc.
To stop this will require 1 extra line of code in your LotusScript:NotesUIDocument.AutoReload = False
So, your code will look as follows:
Dim ws as New NotesUIWorkspace
Dim uidoc as NotesUIDocument
Dim doc as NotesDocument
 
Set uidoc = ws.CurrentDocument
uidoc.AutoReload = False
Set doc = uidoc.Document
 
Call uidoc.Close

In the event that you have to first save the uidoc in your Code, then end it will look like so:

Dim ws as New NotesUIWorkspace
Dim uidoc as NotesUIDocument
Dim doc as NotesDocument
 
Set uidoc = ws.CurrentDocument
uidoc.AutoReload = False
Call uidoc.Save

Set doc = uidoc.Document
 
Call uidoc.Close
 I hope this helps.
Cheers for now
John
12