- Use error trapping
- Use defensive coding
- Protect your code
- Use NotesDateTime instead of strings
- Use DXL as Transport
- Use wizard interface in your Notes client
- Consuming Web services
- Use classes
- Use the Evaluate command
- Use "trusted servers"
|Everyday LotusScript Tip #1. Use error trapping|
Error trapping tests a particular condition when running a program. If the program runs into an error, it will then execute a corresponding routine to fix the error. This should always be done. Error handling is not a joke -- it should always be handled in a serious manner and should always be mandatory.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
There are two ways to go about this:
- Use a single error handler at the top of your LotusScript code. It is simple to implement, but at times difficult to keep in context.
- Implement your error handler at the function level. It's a little bit more work, but it's also much more granular.
|Everyday LotusScript Tip #2. Use defensive coding|
While it may seem a bit paranoid, you should practice using defensive LotusScript coding -- it will save you time in the long run. Always assume the worst and check all inputs on every function. It doesn't usually affect performance, but it is a good failsafe.
Here is a good example of defensive LotusScript coding in a typical function.
Function mytest (p1as String, p2 as String) as integer mytest = false if p1 = "" then exit function if p2 = "" then exit function . . . ' Now actually do something! . . . . mytest = true end function
|Everyday LotusScript Tip #3. Protect your code|
When creating commercial applications, it's a very good idea to hide your code. But, you may be asking how. There are actually two ways you can go about it:
- Create a template and click on "hide design." This is easy to do, but it may end up allowing form customization.
- You could also remove your LotusScript source code from your script libraries. Use the NotesNoteCollection command to find your script design document. Then replace the "$ScriptLib" with a String -- "Hello." This is not the easiest way to go about this process, but your other design elements can be modified as well. (Do not do this on your development copy!)
|Everyday LotusScript Tip #4. Use NotesDateTime instead of strings|
You should never store date/time values as strings. It is always good practice to use NotesDateTime structures instead and save them.
You might say, sure, but why? Well, you never know how the client will interpret dates. Is it dd/mm/yyyy or mm/dd/yyyy? It also means that views will be able to sort on dates.
Trust me, this is a good tip to practice. This issue comes up more often than you might think.
|Everyday LotusScript Tip #5. Use DXL as Transport|
A good reason to consider using DXL as Transport stems from a situation where a customer wants to easily send back "log" documents. When this happens, you can use a LotusScript agent to:
- Pick up all selected documents.
- Create a memo with a rich-text field.
- Use DXL to store the documents in the rich-text field.
At the receiving end, this will:
- Unpack the mail message to a DXL stream.
- Construct new documents to store the data.
This way, you are transferring data without replication. Below is a sample code of this being implemented.
Dim sSession As New NotesSession Dim dbThis As notesDatabase Set dbThis = sSession.CurrentDatabase Dim dc As NotesDocumentCollection Set dc = dbThis.UnprocessedDocuments If (dc Is Nothing) Then exit sub If (dc.count < 1) Then exit sub Dim doc As NotesDocument Set doc = dc.GetFirstDocument While (Not doc Is Nothing) Dim de As NotesDXLExporter Set de = sSession.CreateDXLExporter() Call de.setInput(doc) Dim dxl As String dxl = de.Export ' continued overleaf.. Dim dbMail As New NotesDatabase("", "") Call dbMail.OpenMail() Dim docM As NotesDocument Set docM = dbMail.CreateDocument Call docM.ReplaceItemValue ("Form", "Memo") Call docM.ReplaceItemValue ("Recipients", "email@example.com") Call docM.ReplaceItemValue ("SendTo", "firstname.lastname@example.org") Call docM.ReplaceItemValue ("Subject", "Log Documents") Dim rt As New NotesRichTextItem (docM, "Body") Call rt.AppendText(dxl) Call docM.Send(False) Set docM = Nothing Set de = Nothing Set doc = dc.GetNextDocument(doc) Wend
|Everyday LotusScript Tip #6. Use a wizard interface in your Notes client|
When using a wizard interface with your Lotus Notes client, there are a few steps you should follow:
- Create a form with a tabbed table.
- Set the tabs to "1," "2," "3," etc.
- Select "Switch Rows Programmatically."
- Set the "name" field to the name of the table; for example: "RequestTable."
- Create a variable on the form with $Name; for example: "$RequestTable."
- Have your "forward" and "back" buttons increment/decrement the variable.
|Everyday LotusScript Tip #7. Consuming Web services|
There are two different ways you can go about consuming Web services. The first is quick and to accomplish it, you should follow these steps:
- Install Microsoft SOAP on client machines.
- Write LotusScript to create a Microsoft SOAP object. This is a good option because it is quick and handy when it comes to testing. Unfortunately, it is platform-specific, requires dynamic link libraries on clients, and there is no timeout.
Below is some code that illustrates how to create the Microsoft SOAP object.
Dim Client As Variant Set Client = CreateObject("MSSOAP.SoapClient") 'Initialize connection to the Web Service Call Client.mssoapinit ("http://localhost/testWS.nsf/Simple?wsdl") 'Call our simple GetEmailAddress function provided by Web service Dim result As String result = Client.getJoke() 'output result to message box Messagebox result, 48, "Get Joke"
The other approach is a little different. It's big and robust and uses Stubby. Just point it at a Web service and it produces the code for you.
Some good points about it are that it is multi-platform, scalable and there are no dynamic link libraries. However, it does require you to use more than four lines of code.
|Everyday LotusScript Tip #8. Use classes|
When developing with LotusScript, it is always a good idea to use classes. Here are some reasons why:
- Classes help to bundle data and code in one place.
- They decompose problems into "objects."
- They help to write smaller, more focused code.
- They help define and implement the internal data model.
- They aid reusability.
Classes have a good design methodology, which leads to Java. But everyone is not used to them and it may take time to sink in. Below you will see some code that implements classes.
Class Person private nName as NotesName private strUNID as String sub new(strNewName as string, strNewUNID asString) me.nnName = new NotesName(strNewName) me.strUNID = strNewUNID end sub public function getName as String if (me.nnName is nothing) then exit function getName = nnName.Canonical end function public function getUNID as String getUNID = strUNID end function end class
|Everyday LotusScript Tip #9. Use the Evaluate command|
The Evaluate command allows you to run @Functions within LotusScript. It is sometimes quicker and easier, as it allows you to use your favorite function in certain situations.
An example of it might be:
Don't overuse it though. Loads of LotusScript functions mimic @functions.
|Everyday LotusScript Tip #10. Use "trusted servers"|
It is good practice to use trusted servers because scheduled agents cannot normally open databases on other servers.
The "trusted servers" field in a Lotus Domino R6 server document's security section allows servers to trust other servers. By doing this, it allows you to centralize "collection" agents. You also simplify your architecture and limit the number of agents you use. However, it does rely on a fast, reliable network infrastructure.
As a final note, make sure to never trust servers in another domain.
TUTORIAL: 30 LOTUSSCRIPT TIPS
Part 1: 10 fundamental LotusScript tips
Part 2: 10 everyday LotusScript tips
Part 3: 10 advanced LotusScript tips
Part 4: More LotusScript learning resources