Article: Dynamic Lotusscript »
FERDY CHRISTANT - FEB 11, 2005 (06:18:34 PM)
Introduction
This short article gives an introduction to the underrated Execute method that is available in Lotusscript. The basics are explained through some simple examples, after which two suggestions are made for advanced usage.
The basics
The Execute method in Lotusscript is an underused feature that can be extremely powerful when used properly. This method allows you to dynamically load, execute, and unload Lotusscript modules. The syntax is extremely simple:
Execute ( text )
Where text is a string containing the script to execute. Here's a simple example:
Sub Initialize
Dim strCode As String
strCode = | msgbox "I'm dynamic!" |
Execute( strCode )
End Sub
When you run this code, it will display a message box that says "I'm dynamic!". Now let's have a look at a dynamic script that spans multiple lines:
Sub Initialize
Dim strCode As String
strCode = |
Dim strMessage as String
strMessage = "I'm dynamic"
msgbox strMessage
|
Execute( strCode )
End Sub
Note that pipeline characters are used to span multiple lines of Lotusscript into the strCode variable. This second example has exactly the same effect as the first, yet this time the dynamic code spans multiple lines and the message to prompt is defined in a variable.
The essence of the Execute method should be clear by now; one can dynamically run Lotusscript at runtime. The next two paragraphs demonstrate two interesting applications of this feature.
Dynamic class loading
Besides the simple series of statements from the previous examples, one can also include the Use and Declare keywords in the dynamic script. Consider the following script:
Sub Initialize
Dim strCode as String
strCode = |
Use "libArrayManager"
Dim AM as ArrayManager
Set AM = new ArrayManager()
|
Execute strCode
End Sub
This dynamic code loads a custom fictional class ArrayManager and instantiates a new ArrayManager object from it. The point in this example is that in line 4 we are dynamically loading a module. Thus, at runtime, we can programatically decide to load or not load a module. This technique is called Dynamic Class Loading. When applied correctly this technique can lead to performance gains, because only modules that are needed are actually loaded.
This is not a new technique, I have been using it successfully for years. The first time it appeared was in the must-read redbook Performance Considerations for Domino Applications. Bill Buchan has presented it at Lotussphere '05 as well, so I'll refer you to those two sources for the full details.
Web services
The second advanced application of the Execute method comes quite close to Web Services. One of the key concepts of a web service is that you can dynamically call functions (services) and retrieve the return values, if any. The other concept is that you can do this from anywhere, independant of your calling client, OS or hardware.
Allow me to demonstrate how you can realize the first concept through a simple example:
Declarations
Dim strReturnVal as String
Sub Initialize
Dim strCode as String
Dim strModule as String
Dim strFunction as String
Dim strParam as String
strModule = "libUser"
strFunction = "getCountry"
strParam = "Orky Dorky"
strCode = |
Use | & strModule & |
strReturnVal = | & strFunction & |(| & strParam & |)|
Execute strCode
Msgbox strReturnVal
End Sub
Look at that. Everything is dynamic, the module to load, the function to execute, and the parameter to pass on. Pay particular attention to the bold line in the example. Key concept here is that we can communicate between our static and dynamic script through variables. Note that this only works for globally declared variables, that's why strReturnVal is declared in the top line of the example.
So how does this come close to a web service? We have proven that we can call a function and pass parameters based on data. In the example, this data is defined in variables, but they might have been coming from a SOAP message, email, or Notes document.
Of course, the example is too simple. Not all functions have a return value. Not all functions have exactly one parameter of that data type. Just think of the base concept, the road for web services is paved. Available from Notes 4.6, go figure!



Comments: 9
Reviews: 4
Average rating:
Highest rating: 5
Lowest rating: 4
COMMENT: SPUG
FEB 14, 00:57:59
COMMENT: INGO BEYER

FEB 14, 11:42:20
but you have a little error in your last example.
Msgbox strVal should be Msgbox strReturnVal, or?
Good idea
Ingo «
COMMENT: FERDY
FEB 14, 18:50:38
Ingo, thank you, got that fixed now. «
COMMENT: DON
FEB 17, 07:48:30
problem with building web services using Execute is that it won't hop over database or even server barriers easily. I was thinking of building a LS or LS2J library and accessing it from other databases or even servers.
But maybe I'm wrong and someone has a good approach on this. «
COMMENT: DON

FEB 17, 10:13:37
COMMENT: ANDREI KOUVCHINNIKOV
JUL 4, 11:46:03
COMMENT: TOM
NOV 13, 13:47:43
COMMENT: PRASOB
DEC 8, 2008 - 10:00:28 AM
COMMENT: TOM
FEB 1, 2012 - 02:04:14