Saflet101Variables
Contents |
Using Variables
Variables
Variables can be created through variables view pane. There are three types of variables: Runtime, Local and Global. Runtime variables are utility variables that exist only when a Saflet is executing (These variables might include Asterisk and connection information). In most situations you will will be working with Local and Global variables.
Local variables exist in a Saflet throughout its execution. A Local variable will be initialized with the initial value you assign it. Local variables are accessible to all the ActionSteps within a Saflet.
Walkthrough
Local Variables
For this walkthrough; you will need to be connected to a SafiServer (see Connection to SafiServer from SafiWorkshop)
The first thing we need to do is create a Project and Saflet (See: Creating a Project and Saflet). Make sure to choose Custom Initiator from the Initiator list (By doing this; we will be able top invoke the Saflet without making a phone call)
Now, let's create a Local Variable. In the Variables pane; right-click Local and select Create Variable.
This will pop up the Create New Local Variabledialog. In this dialog you will be able to assign variable name, type and initial value.
Create the following variables:
- varA: Integer with initial value of -1
- varB: Text with initial value of B
- varC: Boolean with initial value of false
The variables you created will appear in variables view:
Global Variables
Global Variables exist at the server level and are accessible to all Saflets and ActionSteps contained within a given SafiServer. In order to create and manage global variables; you will need to be connected to a SafiServer.
Create a global variable by right-clicking the Global node in the Variables view and select Create Variable:
Like the Local variable creation; this will pop up the Create New Global Variabledialog. In this dialog you will be able to assign variable name, type and initial value.
Create the following global variables:
- globalVarA: Integer with initial value of -1
- globalVarB: Text with initial value of foo
- globalVarC: Boolean with initial value of true
Any created global variables will show up in variables view pane (when connected to a SafiServer):
Assignment ActionStep
In this example we are going to assign a value into local variable using the Assignment ActionStep.
From Palette; select the Assignment ActionStep (under the Logic folder) and place into Saflet (by first clicking the ActionStep and then clicking in the workarea). Connect the Custom Inititator step to the newly placed Assignment step. You should end up with something that looks like this:
Double click the Assignment step to bring up the edit dialog:
Note: Each ActionStep has an associated edit dialog that can be accessed by double-clicking on the ActionStep)
Click the edit button ("...") on the left side of the Variable field. This will bring up the Dynamic Value Editor. In the dialog, select varA as the variable and press OK to finish:
Now click the edit button on the left side of the Value field. This will bring up the DVE and you have the option of selecting Variable or Script (in the tabs). For this walkthrough select the Global variable globalVarA and press OK to finish:
You should now see the following variable assignment:
For debugging purpose, let's add two DebugLog ActionSteps (found in the Misc folder in the Palette):
Drag the anchors from the exit points of an ActionStep to another; and connect the step like this:
Now edit the Message field in each of the DebugLog ActionSteps. Do this by double-clicking the ActionStep and selecting the edit button on the Message field in the dialog. For each of these steps; select varA:
Debug
It's now time to test Saflet we built. Save you're Saflet and then select Debug Current Saflet from the Debug menu (you will need to be connected to a SafiServer):
If you haven't published this Saflet to the server, you will be prompted to do so. Select Yes and OK:
Once debugging mode starts; the Saflet will be reopened in a new tab with a pink background:
Invoke the Saflet
Now it is time to invoke the Saflet. Bring up Custom Initiator Launcher by going to the Debug menu and selecting Custom Initiator Launcher:
In Custom Initiator Client dialog; specify Saflet you want to invoke and click send button. This will invoke custom initiator event in SafiServer and will run the Saflet you specified:
By clicking Debug Step Button
, you will be able to control the Saflet step by step. The current step will be highlighted in yellow:
In execution of DebugLog ActionStep, you will be able to see the output of the Message field in the Console view. Since we are keep track of the variable varA, the output will be the value of variable. Step through the first DebugLog and you should see the following:
Step through the Assignment step and the next DebugLog step and you should see the following in the console:
After the Saflet has completed, you will be alerted and the background of debug Saflet will turn gray. You can close this tab.
The Local variables will be reinitialized whenever a Saflet executes with user specified initial value. However, global variables, if changed during execution, will stay as changed value.
Change the Assignment step so that it assigns the value of Local variable to global variable:
Before Saflet execution, variable values are:
After execution of this Saflet, the global variable will stay changed. Since global variables are persisted in the SafiServer database; they will stay set even after server restarts.
If the change is not imediatlly reflected in the Variables view pane, you can refresh it by pressing the refresh button on the SafiServer View tab.
Scripting
Another way to assign a value to a variable is through the use scripting.
In Assignment ActionStep, bring up Dynamic Value Editor for the Value field and select the Script tab. in the edit area enter the following script:
var a=Math.random()*100; var c=Math.round(a); c;
In this example; we are generating a random value, rounding it and returning it. The last line in a Script is what's returned; so in this example c; is evaluated and returned. After pressing OK; you should see that the Value field has changed to: Script: c; :
After execution of Saflet, you should be able to see assigned random value in global variable:
Note on Global Variable Assignment in Script
To assign value to Global variable in script editor, user must use the setGlobalVariable() function. Example:
setGlobalVariable("a", 1); //will assign value 1 to global variable named a. :
SideBar
User Login
Comments (