Variables

Variables are placeholders that store values that can change. When a script runs, variables update with new information. For example, ANI is a variable that holds the caller's ANIClosed Also known as caller ID. Listed phone number of an incoming voice call., or phone number. When ANI appears in a script, it pulls in and holds the caller ID. Each time the script runs, it handles a different caller, so the value of ANI changes.

Variables can be: 

Key Facts about Variables

When using variables in Studio scripts: 

  • Assign them using this syntax:

    varName = value

  • Use the keyword ASSIGN if you have older Snippet code in your script. This provides backward-compatibility. For example, ASSIGN varName = value.

  • Variables are implicitly typed. This means you don't need to define the data type for any variables that you use. For example, varX can be assigned a string value in one statement (varX = "hello") and an integer value in another statement (varX = 1999. The Snippet compiler determines the type each time it encounters a variable.
  • Name variables following these guidelines: 

    • Use alpha-numeric characters (a-z, A-Z, 0-9). The first character in the name must be a letter.

    • Use the underscore character (_) and the dollar sign ($) anywhere in the name.

    • If the dollar sign is the last character in a variable name, all boolean operations (=, >, <, >=, <=) using that variable treat the value as text. For example:

      a$ = 20
      b = 100
      x = a$ > b

      In this example, the variable x is 1 (true). Because the variable a$ ends with a dollar sign, the value (20) is treated as text. This means that a$ > b is a comparison of 20 and 100 as strings of text and not integers. When comparing as text, 20  is larger than 100 because 20 starts with a 2 and 100 starts with a 1. Without the dollar sign in the variable name, x = a > b is 0 (false) because the values are compared as integers, and 20 is not larger than 100.

  • Assign variables date/time values using this syntax:

    #"<date or time>", where <date or time> is the date/time you want to store in the variable. For example, date1 = #"7/15/2050 5:00pm".

  • Use the date/time syntax to perform operations on the date/time values. For example, you can compare them:

    date1 = #"7/15/2050 5:00pm"
    date2 = #"7/15/2050 4pm"
    x = date1 > date2

    The result is 1 (true) because the literal value of date1 is greater than the literal value of date2.

  • Reference the value stored in a string variable by enclosing the variable name in curly braces. For example, The customer's name is {customerName} .becomes "The customer's name is Fenrir Lokison."

Reserved Variables

Reserved variables are read-only, which means a script cannot save values to them. These variables always return a certain kind of information. Many of them return information about the current date or time. Studio has 10 reserved variables.

Pre-Defined Variables

Pre-defined variables are already defined, so you don't need to declare them.

User-Defined Variables

You can define string and numeric variables. Because these variables are implicitly typed, the Snippet compiler determines the type of each variable when compiles the script. This means that the same variable is treated differently depending on how you use it in your script. The following table shows examples of each type of variable based on what you want to do with the variable.

You can also create dynamic data variables.

Global Variables

A global variable allows you to use one variable in multiple scripts during the same contact. They're commonly used in subscripts or spawned scripts. You can create a global variable using an Assignaction or a Snippetaction. Include the keyword Global: before the variable name.

This example shows two ways to create a global variable with the Assign action: 

Global:<variable name>
Global:<variable name>="{firstname} {lastname}"

This example shows two ways to create a global variable with the Snippet action: 

ASSIGN global:<variable name>
ASSIGN global:<variable name>="{firstname} {lastname}"	

Dynamic Data Variables

Dynamic data variables don't have defined types. You can use them to store any type of data. Other Snippet variables are implicitly typed, which means the type is determined when the Snippet compiler compiles the code. The type of dynamic variables is determined when the script runs.

Dynamic variables can hold more than one key/value pair. You can use the variable in any Studio action in the script and access the value you need using the corresponding key. This allows you to reduce the number of variables you use in a script. For example, instead of creating three unique variables, you can create a single dynamic variable: 

DYNAMIC beowulfCharacteristics
beowulfCharacteristics.name = "Beowulf"
beowulfCharacteristics.occupation= "Hero"
beowulfCharacteristics.foe = "Grendel"

You can use dynamic data variables to: 

The number of dynamic data variables and objects in a script can impact tracing. You may see performance issues for scripts that contain a large number of dynamic variables. The more data they contain, the longer it can take to process each action.

Declare Dynamic Variables

To declare a dynamic data variable, use the keyword DYNAMIC in your code before the variable name, then add attributes to it. For example: 

DYNAMIC beowulfCharacteristics
beowulfCharacteristics.name = "Beowulf"
beowulfCharacteristics.occupation= "Hero"
beowulfCharacteristics.foe = "Grendel"

You can use a dynamic data variable to parse JSON. Define the dynamic variable and use the FROM command to specify the JSON key/value pairs with this syntax: 

DYNAMIC var1 FROM '{ "key1": "apple", "key2": "banana", "key3": "carrot"}'

In Studio, __type (with two underscore characters) is used when parsing JSON. It cannot be used as a key name in dynamic data variables, because they can parse JSON. If you use it as a key name in a dynamic data variable, it will cause an error when you save the script or when the script executes the action.

Access REST Responses

You can use dynamic data variables to access responses from REST calls. These responses are in JSON, and Studio manages them with dynamic variables. For example:

ASSIGN GetRequest = "<API Endpoint>"

ASSIGN DynamicReturn = Proxy.MakeRestRequest(GetRequest,"",0,"GET")

ASSIGN fileContent = DynamicReturn.files.file

In this example, MakeRestRequest returns a dynamic data object. You can parse the returned response using the DYNAMIC var1 FROM '{ "key1": "apple", "key2": "banana", "key3": "carrot"}' syntax.

Generate REST API Calls

Generate REST API calls and send them as JSON using the asjson() method. For example:

DYNAMIC TokenInput
TokenInput.grant_type = "password"
TokenInput.username = "Grendel.Cainson"
TokenInput.password = "MadeUpPassword"
<additional TokenInput properties>
TokenJsonInput = "{TokenInput.asjson()}"
ASSIGN proxy = GETRESTProxy()
<ASSIGN additional variables>
ASSIGN TokenResponse = proxy.MakeRestRequest(TokenRequestURL,TokenJsonInput, 0, "POST")

In this example, TokenInput is declared as a dynamic variable with three attributes, grant_type, username, and password. TokenJsonInput is declared to hold TokenInput in a stringified form using the asjson() method. In the last line of the example, the variable TokenResponse is declared to hold the REST request, which can then be used in the script code to send the request.

Implicit Typing

Variables in the Snippet language are implicitly typed. You don't need to declare the type when you declare a variable. You can declare and assign a variable in one step. For example, x = 10. Implicit typing means that variables can store different types of data.

By default, numeric values are always evaluated as numbers. For example: 

a1 = "100"
a2 = "100.00"
y = (a1 = a2)

The result here is y = 1 (true) when the values are compared as numbers. If these values were compared as text, the result would be y = 0 (false). Because these values are both numbers, this expression is evaluated numerically.

Force Comparison as Text

You can force a value to be treated as text even if it's numeric. You can do this in two ways:

  • Add a dollar sign to the end of the variable. For example, if you want a1 = "100" to be evaluated as text, you would add a dollar sign to make it a1$ = "100".
  • Surround the names of the variables with curly braces. For example, y = "{a1}" = "{a2}".

Force Comparison as Date or Time

You can add a pound sign (#) in front of a value if you want it to be recognized as a date or time. You can use this syntax to perform operations on dates and times that you wouldn't otherwise be able to do. For example, you can compare them:

date1 = #"7/15/2050 5:00pm"
date2 = #"7/15/2050 4pm"
x = date1 > date2

The result is 1 (true) because the literal value of date1 is greater than the literal value of date2.

Variable Redaction

Variable redaction eliminates variable values from traces and logs generated by a script.

Redaction is configured at the script level in the VariableRedaction field of a script's properties. Redacted values are replaced with a string of X characters. The length of the variable value determines how many X's are used, meaning that a redacted five-letter word would result in five X's. Partial variable redaction, such as part of a credit card number, is not supported.

Variable redaction occurs at the script level. It isn't an inheritable property. If a redacted variable is passed to other scripts, such as with a RunScript or RunSub action, that variable isn't automatically redacted in the subsequent scripts. If you want a variable to always be redacted, you must configure variable redaction in all scripts it might be passed to.

  1. In Studio, click on the script canvas to make sure nothing is selected.
  2. Click the Properties tab.
  3. Click the ellipsis button next to the VariableRedaction field to open the String Collection Editor.
  4. Enter the names of the variables you want to redact. These names should correspond to variables created in Assign actions in the script.
  5. Click OK.