Flow Control Command Set
The Snippet language (created by NICE CXone) contains a set of basic commands for flow control. These include REPEAT, FOR, IF, SWITCH, and so forth. All of these commands are designed to change the order of the linear sequence of statements being executed. Each command requires the conditional code to be enclosed in curly braces, even if the conditional code is just one line. For example:
IF x=1 { TRACE "one" }
When the compiler encounters a new line or an open curly brace, it recognizes the end of a statement. The following formats are valid (including the preceding example):
IF x=1 { TRACE "one" } or IF x=1 { TRACE "one" } or IF x=1 {TRACE "one" } IF x=2 {TRACE "two" }
Commands
This section details each command. For examples, the following style will be used to describe the syntax and options of the command:
COMMAND <expression> [ <optional> ] { ... }
The name of the command is shown in all caps and designates the key word (or words) required by the command (in practice, command names are not case sensitive). Portions inside angle brackets (< >) indicate a specific expression or value provided by the user. The name in italics will be associated with a description found below. The name and angle brackets are never to be entered literally, rather that portion should be replaced by the scripter. Portions enclosed in square brackets ([ ]) are optional, but if used, must appear in the location indicated. Again, the square brackets themselves should not be entered. Where curly braces are shown, they are to be entered as indicated and the ellipses (...) designates a place where other Snippet statements may be entered. Note that a single statement may be placed on one line, but if multiple statements are required, a new line will need to separate them.
ASSIGN
ASSIGN <name>=<value>
Allows a variable to be assigned a value. The syntax of the value matches the syntax supported by the Assign action.
When working with expressions, it is important to remember that the equal sign (=) does not assign a value to the variable on the left side of the expression. In algebra and in other programming languages, it is common to represent a variable assignment by using the equal sign: x = 2 * y - 3. This algebraic expression suggests that x will take on the value of 2 * y - 3. If this same expression were used in the If action, the variable x would be compared with the value of 2 * y - 3. To change the value of a variable, use the Assign action instead.
IF
IF <expression> { ... } [ ELSE { ... } ]
This command evaluates the expression and conditionally executes the code within the initial curly braces ({…}). A true result is any non-zero value, a false result is a zero value. Boolean operators such as =, >, <, >=, <=, and != compute 1 for true and 0 for false. The optional ELSE clause will be executed if the expression evaluates to false (zero). Parenthesis surrounding the expression are not required, but are not forbidden either. For example:
IF x=1 { skill = 123 } ELSE { skill = 456 }
The following are the valid Boolean operators and their meaning:
Operator | Details |
---|---|
= | Equal. Compares the two expressions on either side, if they are equivalent, the expression results in 1, otherwise the expression results in 0. |
!= | Not equal. |
< | Less-than. Compares the two expressions on either side, if the left side is less than the right, the expression results in 1, otherwise 0. Textual comparisons use alphabetical order to determine which is greater or less-than. |
> | Greater-than. |
<= | Less-than-or-equal. |
>= | Greater-than-or-equal. |
& | And. If the two expressions on either side both evaluate to non-zero, the expression results in 1, otherwise 0. |
| | Or. If either expression evaluates to non-zero, the expression results in 1, otherwise 0. |
() | Parenthesis. Defines a sub-expression which will be evaluated as a unit. |
Regular mathematical order or precedence applies. Multiplication and division are higher priority than addition and subtraction. Comparison operators are below math operators.
In addition to simple math and comparison operators, the modulus operator (%), power (^), and integer divide (\) are supported as well as an exclusive or operator (~).
Expressions are used in If and Snippet actions. Refer to the script examples in those topics to see how expressions are correctly used. The following is a typical expression to compare the variable "customer" with a string of text:
customer = "Mary Joe"
When evaluated as an expression, it results in a True or False value. When customer contains the exact value "Mary Joe", the result will be True. For any other value, the result will be False. If is the most common action to make use of expressions and contains one property: Expression. The previous example can be entered into the Expression property. When the application runs, the value of customer will be compared with the string "Mary Joe". If it matches, the result condition True will be followed.
REPEAT
REPEAT <expression> { ... [ BREAK ] }
This command repeats the code within the curly braces the number of times indicated by expression. This can be any expression that evaluates to zero or a positive number. The optional BREAK clause will abort the loop and continue execution below the closing curly brace. See the following REPEAT example:
REPEAT 10 { phone = "{phone}{random(10)}" }
SWITCH
SWITCH <expression> { CASE <literal> [ { ... } ] [ CASE <literal> { ... } ] [ DEFAULT { ... } ] }
This command evaluates expression and branches to the code indicated by the CASE with the matching literal value. The literal value cannot be an expression, it must be a literal number or text. At least one CASE is expected, but there is no pre-defined limit to how many cases may exist. The optional DEFAULT code will be executed if no CASEs match and must be the last clause in the SWITCH.
For every CASE within a given SWITCH, the type of literal must be the same. The first CASE determines the type required by the remaining. If the type changes from that of the first CASE, a compile error will be raised. The following are all examples of valid literal types:
CASE "john" CASE 512 CASE #"5/15/2050" CASE #"6/1/2050 7:00am" CASE #"7am"
Example of CASE:
SWITCH x { CASE 1 { skill=1234 } CASE 2 { skill=2345 name="Help" } DEFAULT { skill=4567 } }
Several cases may be listed without an associated code block to cause multiple matches to all execute the same block. This is called case fall-through and is permitted as long as there is a case encountered which has a code block. Here is an example: (For 1, 2, and 3 cases, the value "hi" will be output. For 5 and 6, "bye" will be output)
SWITCH x { CASE 1 CASE 2 CASE 3 { TRACE "hi" } CASE 5 CASE 6 { TRACE "bye" } }
SELECT
SELECT { CASE <expression> [ { ... } ] [ CASE <expression> { ... } ] [ DEFAULT { ... } ] }
This command evaluates each expression from first to last until it finds the first non-zero (true) result and then executes the code within the associated curly braces. If no expression results in non-zero, the optional DEFAULT code will be executed. At least one CASE is expected, but there is no pre-defined limit to how many CASEs may exist. The optional DEFAULT clause must be the last clause in the SELECT. Example of SELECT:
SELECT { CASE name="Odin" { agentid = 123 } CASE name="Frigg" { agentid = 345 } CASE name.length = 0 { agentid = -1 error = "Invalid name" } DEFAULT { agentid = 999 } }
FOREACH
FOREACH <var> in <array> { ... [ BREAK ] }
This command repeats the code within the curly braces for each element of array. The value of each array element is assigned to a variable named var. An optional BREAK clause will abort the loop and continue execution below the closing curly brace. Example of FOREACH:
a[1] = "Odin" a[2] = "Loki" a[3] = "Thor" FOREACH item IN a { TRACE "Item = {item}" }
Output:
Item = Odin
Item = Loki
Item = Thor
FOR
FOR <var> = <start> TO <end> { ... [BREAK ] }
This command repeats the code within the curly braces once for each whole value from the range of start through end. The start and end clauses may be expressions or literal values. For each value, a variable named var will be assigned to the current value in the range (called the iterator variable). The value of start and end will be evaluated once, at the beginning of the loop.
The value of start need not be less than end. If end is less than start, the loop will traverse downward. This may be counter-intuitive for those familiar with other languages. But it is important to recognize that at least one iteration of the loop will always occur. Also note that it is acceptable to alter the iterator variable from within the loop. Example of FOR:
FOR i=1 TO 9 { ph = "{ph}{i}" } Result: ph="123456789" FOR i=9 TO 1 { ph = "{ph}{i}" } Result: ph="987654321"
FUNCTION
FUNCTION <name>([<parameter>][...]) {RETURN <data> ]}
Declares a reusable function. You can also call this reusable function with the following syntax:
<name>([<parameter>[<parameter>][...])
TRACE
TRACE "<text>"
Outputs text to the result window of the Snippet Properties window.