myContactCenterManual

Code in flows

How to calculate and decide with C# in flows — the Script code node, functions and the expression of a branch — what the code sees, how the flow designer checks it and what happens when errors occur during operation.

In a flow, C# code appears in three places:

Place What you write Result
Script code node statements that return a text with return The text determines at which output the flow continues.
Functions your own methods with parameters and return value can be called from script code, expressions and other functions
Expression of a Branch a condition, for example Position > 10 The flow continues at True or False.

The flow designer assembles all the code of a flow into one common class. The Routing Service compiles it as soon as it receives the flow and runs a separate instance for each conversation. Variables therefore always apply to one conversation only.

The Script code node

You find the Script code node in the toolbox of the flow designer under Extended Nodes, in every kind of flow. Its properties:

Property Meaning
Display Name the name in the flow
Outputs the outputs; “+” adds one, “–” removes one. Each Output has a Display text and can be Visible.
Script the code; “…” opens the code editor

The code is the body of a method that returns a text. The flow continues at the connected output whose Display text matches exactly this text — case and spaces count.

Functions

You create functions in the properties of the flow — with no node selected — under Global Elements › Functions:

Property Meaning
Name a valid C# name: without spaces or special characters, not starting with a digit
Return Type int, float, double, bool, string, DateTime, TimeSpan or void
Parameters Name and Type for each parameter: the same types and long
Code the body of the method; “…” opens the code editor

You call a function like a method, for example IsServiceNumber(DestinationNumber). Functions without parameters that return a value are also available as placeholders in texts of the flow: %Name% — see Placeholders.

The expression of a branch

The Branch node has the Expression property: a single-line C# condition that yields true or false. If it is true, the flow continues at the True output, otherwise at False. The expression has access to the same names as script code.

What the code sees

  • Variables of the flow — under Global Elements › Variables — as fields that you read and write: Count = Count + 1;. You address subfields with a period, such as Customer.Name.
  • System variables and functions such as Position, IsOpen() or AvailableAgents("Sales", "English", "InboundCall") — the same as in texts, see Placeholders, but in code also with several arguments. In code, they have a type: IsOpen(), for example, yields bool, Position a number.
  • Skill and Language can also be set, with the name of the skill or the language: Skill = "Sales";. The flow treats a name that does not exist as an error.
  • trace writes to the log: trace.Info(…), trace.Warning(…), trace.Error(…). The lines appear in the Flow area of the Routing Service, see Tracing Settings.

The Flow variables selection below the code editor lists all these names with Return Type, Parameters and Description and inserts the selected one at the cursor.

The namespaces included are, among others, System, System.Collections.Generic, System.Linq, System.Threading.Tasks, System.Data, System.Data.Odbc, System.Data.SqlClient and MimeKit. Your own using lines are not possible; write everything else with its full name, such as System.Text.RegularExpressions.Regex. The code does not run asynchronously — there is no await.

Checking and saving

The flow designer compiles the whole flow with every change. Errors are listed in the error list below the diagram and in the code editor. Double-clicking an error in the code opens the editor at the right place.

Code Message
UC0001 Usercode in node contains compiler error. — in the script code or expression of a node
UC0002 Usercode in function contains compiler error.
UC0003 Usercode contains compiler error. or Script in Script Node returns non existing Output Display String. — a return "…" matches no output

The message is followed by the line and the compiler error. As long as one of these errors exists, the flow cannot be saved: the Critical Error window reports Cannot save flow because of critical errors and Please fix these errors to make saving possible.

Generate Code in the ribbon shows all the code that the flow designer generates — helpful for finding a reported line.

During operation

  • Errors: If code throws an exception, the flow reacts with its Error behaviour — for calls, for example, Default: end the call. Catch errors that you want to trace yourself and write them to the log with trace.Error(…).
  • Duration: The code runs in the course of the conversation. While it is calculating, the conversation waits; an endless loop holds it.
  • Time: DateTime.Now is the local time of the computer on which the Routing Service runs.

Examples

Open or closed: A script code node with the outputs “Open” and “Closed”:

if (IsOpen() && AvailableAgents() > 0)
{
    return "Open";
}

return "Closed";

Taking the customer number from the signaling: The phone system passes the customer number in the SIP header field X-CustomerNumber; the flow has a variable CustomerNumber of type string and the outputs “Known” and “Unknown”. GetXHeader expects the name of the header field without “X-”; Trim() removes spaces at the edges of the value:

string number = GetXHeader("CustomerNumber").Trim();
if (string.IsNullOrEmpty(number)) return "Unknown";

CustomerNumber = number;
trace.Info("Customer number from X header: ", number);
return "Known";

Function in a branch: A function IsServiceNumber with the return type bool, a parameter number of type string and the code return number.StartsWith("0800");. The Expression of the branch is IsServiceNumber(DestinationNumber).

Function for texts: A function TimeOfDayGreeting with the return type string, without parameters, with the code return DateTime.Now.Hour < 12 ? "Good morning" : "Good afternoon";. In an announcement from text, you then write %TimeOfDayGreeting%.

    ↑ ↓ select · Enter open · Esc close