Variables
reviewed: 16 February 2025
Setting and Referring to Variables
We have already seen variables in action. You can set a variable using the set() function and refer to a variable by using the square brackets notation.
set( 'variable', 'Hello world' )
response.write( [variable] )
Note that the set() function refers to the variable name as a string. You could thus get some exotic constructs like the following:
set( 'variableName', 'variable' )
set( [variableName], 'Hello world' )
response.write( [variable] )
You can imagine scenario's where the above could be useful especially when developing highly generic modules.
You can also get the value of a variable using the get() function:
set( 'variable', 'Hello world' )
response.write( get( 'variable' ) )
The benefit of get() is that the name of the variable is now a string.
set( 'variableName', 'variable' )
set( [variableName], 'Hello world' )
response.write( get( [variableName] ) )
Variable Scope
Each variable has a scope which defines the visibility of the variable.
By default, the scope of a variable is the function in which it is created. See for example the following example:
function doTest()
set( 'variable', 1 )
response.writeLine( [variable] )
page.call( './called' )
response.writeLine( [variable] )
// Output 1 2 1
end-function
function called()
set( 'variable', 2 )
response.writeLine( [variable] )
end-function
The variable is assigned the value 1 in the function doTest. We invoked the function called in which we assign 2 to a variable with the same name. When we check the value of the variable after the function call,we see the value is still 1.
The function called has its own variable scope and the variable has the same name but is an entire different variable. We have learned before that you do not have to define a variable in CaseMaster so when we refer to the variable in the function called the value will be null.
Parameters can be referred to as local variables in a function. See the following example:
function doTest()
set( 'variable', 1 )
response.writeLine( [variable] )
page.call( './called', [variable] )
response.writeLine( [variable] )
// Displays 1 1 2 1
end-function
function called( v )
response.writeLine( [v] )
set( 'v', 2 )
response.writeLine( [v] )
end-function
We have learned about the isolate construct here. Isolate can be used to create a new variable scope within a function.
The [_me] Exception
There is one important exception to the variable scope as explained in the previous paragraph.
In a BO script you can refer to the variable [_me] in any (non-static) function. This variable is a handle to the current business object; in other OO languages you may see the keywords me or this.
Breaking Out of the Variable Scope
There are a number of tricks you can use in CaseMaster to break out of the variable scope. This is done by prefixing the variable name with a scope directive.
| Scope directive | Usage | Example |
|---|---|---|
| ./ | Standard scope, same as not having a scope prefix | [./variable] |
| ~/ | If local variable exists use that otherwise keep searching in outer scopes | [~/variable] |
| // | Store in request scope; can be referred to anywhere in same (HTTP) request | [//variable] |
| $/ | Store in application scope; can be referred to anywhere in same application. Note that in a server farm or server garden multiple processes are at play and they all have their own application scope | [$/variable] |
Examples
The following example shows the use of the ~/ scope directive. There is no variable with the name variable in the function called so Casemaster searches in the scope of the calling doTest function, finds it there and uses that.
function doTest()
set( 'variable', 1 )
page.call( './called' )
response.writeLine( [variable] )
// Displays 1 1
end-function
function called()
response.writeLine( [~/variable] )
end-function
This example is very similar but note that we create variable in the function called. This means that [~/variable] will find the local variable and use that.
function doTest()
set( 'variable', 1 )
page.call( './called' )
response.writeLine( [variable] )
// Displays 3 1
end-function
function called()
set( 'variable', 3 )
response.writeLine( [~/variable] )
end-function
Note that you setting ~/variable will always create / set a local variable (it will never set a variable in an outer scope).
function doTest()
set( 'variable', 1 )
page.call( './called' )
response.writeLine( [variable] )
// Displays 3 1
end-function
function called()
set( '~/variable', 3 )
response.writeLine( [~/variable] )
end-function
function doTest()
set( '//variable', 1 )
page.call( './called' )
response.writeLine( [//variable] )
// Displays 1 3 3
end-function
function called()
response.writeLine( [//variable] )
set( '//variable', 3 )
response.writeLine( [//variable] )
end-function
function doTest()
set( '$/variable', 1 )
response.writeLine( [$/variable] )
page.call( './called' )
response.writeLine( [$/variable] )
// Displays 1 3
end-function
function called()
page.call( './calledOneMore' )
end-function
function calledOneMore()
set( '$/variable', 3 )
end-function
<End of document>