Posts Tagged ‘lotusscript’

8 Oct 2013
6

Connect to a Notes Database on a remote Domino Server via SSJS

The other day I found myself needing to access a Notes Database on a Remote Domino Server via SSJS, but for some reason every one of my attempts failed miserably. No error messages written to the Console, no warnings, nothing.

Let’s quickly create a scenario

  • You have 2 Domino Servers: Server1/ACME and Server2/ACME.
  • On both Servers you have a Notes Database called “Test.nsf”.
  • On Server1/ACME you have a Notes Database with an XPage that calls the following SSJS:
1
2
3
4
5
6
7
var db:NotesDatabase = session.getDatabase("Server1/ACME", "Test.nsf");
 
if(db.isOpen()){
   print(db.getTitle());
}else{
   print('Cannot Open Notes Database');
}
  • Now, in the above example, you are trying to access a Notes Database on the same Server. The above code will successfully connect to the Notes Database and print the Database Title to the Server Console.
  • Next up, you have the following code that runs on the same Server:
1
2
3
4
5
6
7
var db:NotesDatabase = session.getDatabase("Server2/ACME", "Test.nsf");
 
if(db.isOpen()){
   print(db.getTitle());
}else{
   print('Cannot Open Notes Database');
}
  • In this example, you are trying to access “Test.nsf” on Server2/ACME, from a Script that’s running on Server1/ACME. This code will fail and will not provide you with any error message. The NotesDatabase Object will simply remain null.

The Reason

In all my years of developing Notes Applications, I never came across having to connect to a Database on a different Server. The reason the connection fails, is because Server1/ACME is not listed as a Trust Server in Server2/ACME’s Server Document in the Domino Directory.

The Solution

To resolve this issue is simple. The difficult part was finding the problem. If you have access to the Domino Directory on Server2/ACME, do the following:

  • Open the Domino Directory (Server Address Book) on Server2/ACME.
  • Under “Configuration”, click “Servers”, then click on “All Server Documents”.
  • Double click on the Server Document you want your code to connect to. (Server2/ACME in this example)
  • Under the Security Tab, in the bottom left section, there is a field called “Trusted Servers”.
  • In my scenario, this field is empty. Following this example, I would add “Server1/ACME” to this field.

Blog1

  • Save and Close.
  • Restart Server2/ACME.

Once you’ve restarted, your code will successfully connect to “Test.nsf” on Server2/ACME.

Till next time….cheers :)
John
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.

28 Aug 2012
3

Important Tip when comparing values in Java

Hi everyone. Here’s a quick post that might save you a lot of time. With some programming languages like JavaScript, @Formula or LotusScript, to compare a String value is pretty simple

@Formula: tmp1 := “Test”;

tmp2 := “Test”;

(tmp1 = tmp2) = True


JavaScript:var tmp1 = “Test”;

var tmp2 = “Test”;

(tmp1 == tmp2) = True

 

LotusScript:dim tmp1 as String

dim tmp2 as Stringtmp1 = “Test”

tmp2 = “Test”

if(tmp1 = tmp2) = True

 

But when it comes to Java, the rules are slightly different:

String tmp1 = “Test”;

String tmp2 = “Other Test”;

(tmp1 == tmp2) = True

 

In Java, the == operator compares Objects and not the physical values contained within. The line above this is checking if the object tmp1 is the same object as tmp2.

So, in Java, the proper way to compare values is by using the equals() method:

String tmp1 = “Test”;

String tmp2 = “Test”;

(tmp1.equals(tmp2) = True

 

If it’s anything that I can teach you today, it’s that with Java almost everything is an Object and not just a data type. If it’s an Object, it has Properties and Methods that should be considered at all times, especially when parsing or comparing values.

Cheers for now :)

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 :)
12