Decision-Making Statements

The information on this help page applies to both CXone Studio and Desktop Studio.

You can have your script select a task to perform based on the conditions you specify. Using a decision-making statement, you can design the script to account for different possibilities or outcomes. This allows you to control the linear flow of what happens in your script.

The keywords for these statements are: 

  • IF: Selects which option to execute based on the result of the evaluation of a BooleanClosed A data type that has two possible values: true and false. expression.
  • SELECT: Executes the first statement in a series of statements that evaluates as true.
  • SWITCH:Evaluates an expression and compares the result to defined cases. It takes the action defined in the matching case.

Decision-making statements always require conditional code blocks. Blocks are enclosed in curly braces ( { } ). The code blocks for SELECT and SWITCH statements use two additional keywords:

  • CASE: Defines one of a set of possible statements to be executed.
  • DEFAULT: Defines the default CASE. This is the statement that's executed if none of the other CASEstatements can be executed.

The discussion of each type of statement includes a syntax summary. These summaries use certain conventions to indicate the parts of the statement and whether each part is required or optional.

IF and IF... ELSE Statements

Description: Evaluates an expression and takes a branch based on the results of the evaluation.

Syntax: IF <expression> <{ ... }> [ ELSE { ... } ]

Studio Action: If (help page in CXone Studio: If)

Details: IF statements evaluate an <expression>. Depending on how the expression evaluates, the script executes the code in the first set of curly braces { ... }. The expression must evaluate as true or the script won't execute the code.

You can include the ELSE statement if you want to specify what the script does when the expression evaluates as false. ELSE is optional. Use it when there are two possible actions and you want the script to choose between them. Use IF alone when there's only one possible action and you want the script to perform it only when the specified condition (<expression>) is met.

Use a BooleanClosed A data type that has two possible values: true and false. operator in the expression <expression> that's part of the IF statement. You must use an expression that evaluates to either true or false. For example, x=5 is a valid expression, because the value of the variable x will either equal 5 or it will not.

Studio does not support having more than one ELSE statement in an IF. However, you can embed IF statements: 


IF A > B {
// Do something
}
ELSE {
	IF A = B {
	// Do something else
	}
	ELSE {
	// Do this
	}
}

Example

IF TEST = 1
   {
      ASSIGN contactID = "123456780"
      ASSIGN customerName = "XYZ International"
      ASSIGN CBWindowStart = #"{date} 8:00 AM"
      ASSIGN CBWindowEnd = #"{date} 4:30 PM"
   }

//String Comparison

IF customerName = "ABC Corporation"
   {
      ASSIGN contractLevel = "Gold"
   }
   ELSE
      {
         ASSIGN contractLevel = "Silver"
      }

//Numeric Comparision

IF contactID % 10 = 0
   {
      ASSIGN logCall = 1
   }
   ELSE
      {
         ASSIGN logCall = 0
      }

//DateTime Comparison

ASSIGN myTime = #"{time}"

IF myTime >= CBWindowStart && myTime  <= CBWindowEnd
   {
      ASSIGN offerCallback = "True"
   }
   ELSE
      {
         ASSIGN offerCallback = "False"
      }

SELECT Statements

Description: Performs a set of commands based the first expression to evaluate to true.

Syntax: SELECT <{ CASE <expression> [{ ... }] [CASE <expression> <{ ... }>] [CASE ...] [DEFAULT <{ ... }>] }>

Details: In a SELECT statement, the script evaluates each CASE <expression> starting at the top and working down until it finds the first that evaluates to true. For the first CASE <expression> to evaluate as true, SELECT executes the code within the curly braces [{ ... }] of that CASE <expression>.

If no CASE <expression> evaluates to true, the script executes the optional DEFAULT { ... } code.

At least one CASE statement is expected. If included, the DEFAULT { ... } statement must be the last clause in the list of CASE statements.

For example: 

SELECT
{
	CASE name="Odin" { agentid = 123 }
	CASE name="Frigg" { agentid = 345 }
	CASE name.length = 0
	 {
	   ASSIGN agentid = -1
	   ASSIGN error = "Invalid name"
	 }
	DEFAULT { ASSIGN agentid = 999 }
} 

SWITCH Statements

Description: Evaluates the specified variable and takes the action defined by the matching CASE.

Syntax: SWITCH <var> <{CASE <literal> [{ ... }] [CASE <literal> [{ ... }]] [CASE ...] [DEFAULT [{ ... }]] }>

Details:  SWITCH statement compares the value of a variable <var> to the literal value of each CASE <literal>. When a matching CASE is found, SWITCH executes and branches to the code in curly braces { ... } associated with the matching CASE <literal>.

If no CASE <literal> matches <var>, the optional DEFAULT { ...} code executes. DEFAULT must be the last clause in the statements embedded in the SWITCH statement.

SWITCH can only evaluate a variable. It cannot evaluate an expression. If you use an expression, your script will not work.

At least one CASE is expected. All CASE statements must have the same type of <literal>. The first CASE determines the type for the remaining CASE statements . If any CASE statement has a different type from the first CASE, a compiler error occurs. The following are all valid literal types:

  • CASE "john"
  • CASE 512
  • CASE #"5/15/2050"
  • CASE #"6/1/2050 7:00am"
  • CASE #"7am"

For example:

IF TEST = 1
{
  ASSIGN MyDate = "07/03/2023" //This date falls on a Monday
}
ASSIGN MyDow = MyDate.asdatedow
SWITCH MyDow
{
  CASE 0   { ASSIGN NewToday = "Sunday"  }
  CASE 1   { ASSIGN NewToday = "Monday"  }
  CASE 2   { ASSIGN NewToday = "Tuesday" }
  CASE 3   { ASSIGN NewToday = "Wednesday" }
  CASE 4   { ASSIGN NewToday = "Thursday" }
  CASE 5   { ASSIGN NewToday = "Friday"  }
  CASE 6   { ASSIGN NewToday = "Saturday"   }
}

If more than one CASE uses the same code { ... }, you don't need to include a code block after each one. Instead, list the CASE keywords one after the other, with the code after the last CASE that uses that code. This is called case fall-through. It's allowed as long as there is a case for SWITCH to encounter that does have a code block. The following example shows case fall-through. For CASEs 1, 2, and 3, the output is the value hi. For CASEs 5 and 6, the output is bye.

SWITCH x
{
	CASE 1
	CASE 2
	CASE 3 { TRACE "hi" }
	CASE 5
	CASE 6 { TRACE "bye" }
}

Use Case: SWITCH allows you to have specific outcomes depending on what the specified variable contains. For example, you can have the script evaluate a variable containing area codes. You can define a CASE for each possible area code and have the script add a value to the state variable that matches the state the area code is assigned to. For example:

SWITCH areaCode
{
	CASE 603 { state =  "New Hampshire" }
	CASE 614 { state =  "Ohio" }
	CASE 628 { state =  "California" }
	CASE 646 { state =  "New York" }
	CASE 667 { state =  "Maryland" }
}

CASE Statements

Description: Defines one of a set of possible statements to be executed. Used with SWITCH or SELECT.

Syntax: varies; see SWITCH or SELECT

Studio Action: (help page in CXone StudioCase)

Details:  CASE must be used in the conditional code block of a SWITCH or SELECT statement. Add one or more CASE statements to define the possible blocks for the script to execute. At least one CASE is required.

Related: Include a DEFAULT statement to define the case that's used if none of the specified CASE statements apply.

DEFAULT Statements

Description: Defines the CASE block that's used if none of the specified CASEs, apply. Used with SWITCH and SELECT.

Syntax: Varies; see SWITCH or SELECT

Details: DEFAULT must be used with SWITCH or SELECT blocks, and there must be at least one CASE statement present. DEFAULT cannot be used on its own. DEFAULT is always optional. It must be the last clause in the list of CASE statements.

ELSE Statements

Description: Defines the statement the script executes if the IF expression evaluates to false.

Syntax: { ELSE ... }; see IF

Details: ELSE is always optional. You can include an ELSE statement if you want to specify what the script does when the expression evaluates to false. Use it when there are two possible actions and you want the script to choose between them.