Additional functions to use in dynamic variables
In advanced actions, you can use custom functions in addition to functions based on MathJS:
String Functions
indexOf(haystack, needle)
If the “needle” string is found in the “haystack” string or list, the function returns its (zero-based) position, otherwise it evaluates to -1.
Examples:
indexOf("ABCD", "A") //returns 0 indexOf("ABCD", "CD") //returns 2 indexOf("ABCD", "E") //returns -1 indexOf(["a","b","c"], "a") //returns 0 indexOf(["a","b","c"], "bc") //returns -1
getIndexValue(haystack, index)
If the “index” number is smaller than the “haystack” list, the function returns the value located at the “index” position, otherwise it will error.
Examples:
getIndexValue(["a","b","c"], 0) //returns a getIndexValue(["a","b","c"], 1) //returns b
substr(string, start[, length])
This function returns a substring of the original string, starting from the zero-based start position. If length is specified, it returns that many characters, and otherwise, it returns the rest of the string.
Examples:
substr("ABCD", 1) //returns "BCD" substr("ABCD", 2, 1) //returns "C" substr("ABCD", 5) //returns ""
slice(string, start[, end])
This function returns a substring of the original string, starting from the zero-based start position. If an end position is provided, it returns all characters up to, but not including, the character at that position. Otherwise, it returns the rest of the string.
Negative positions for start or end are counted from the right side of the string.
Examples:
slice("ABCD", 1) //returns "BCD" slice("ABCD", 0, 2) //returns "AB" slice("ABCD", 1, -1) //returns "BC" slice("ABCD", 2, 1) //returns ""
upper(string)
This function returns the provided string, converted to uppercase.
Example:
upper("aBcD") //returns "ABCD"
lower(string)
This function returns the provided string, converted to lowercase.
Example:
lower("aBcD") //returns "abcd"
length(string)
This function returns the length of the string.
Examples:
length("") //returns 0 length("ABCD") //returns 4
Logical functions
equal(value1, value2)
This function returns true if value1 and value2 are the same value and type, or otherwise, false. Alternative syntax for `value1 === value2`. (`value1 == value2` is a weaker comparison)
Examples:
equal(2, 1) //returns false equal(1, 1) //returns true equal(1, "1") //returns false (different types)
ifElse(condition, valueIfTrue, valueIfFalse)
This function checks the provided condition. If the condition is true (or truthy), it returns the valueIfTrue, otherwise it returns the valueIfFalse.
Examples:
ifElse(equal(1, 5), "equal", "not equal") //returns "not equal" ifElse(equal(2, 2), "equal", "not equal") //returns "equal"
Regex functions
match(value, pattern[, flags, groupIndex])
matchAll(value, pattern, matchIndex[, flags])
These functions perform regular expression matching and testing if the provided “value” matches the regular expression “pattern” provided.
The optional flags argument is a string of single letter regular expression flags that enables advanced behavior (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags). The most common usage for a flag argument is to pass “i” for a case-insensitive match.
There are two related functions here:
- The
match
function looks for a single match and supports an advanced usage with numeric capture groups, using the last argument to select the desired capture group (0 is the whole match, 1 is the first capture group, and so on). - The
matchAll
version supports multiple matches, using thematchIndex
argument to select the desired match. (0 is the first match, 1 is the second match, and so on.)
In both cases, if there is a match the result will be the string content, or when used as the body of a dynamic Boolean variable, it will be true
if there is a match or false
if there was not a match.
Examples:
// Basic usage match("abc", "b.") // returns "bc" (or true as a Dynamic Boolean Variable) match("abc", "c.") // returns no match (or false as a Dynamic Boolean Variable) // Advanced usage // Case-insensitive match("aBc", "b.", "i") // returns "Bc", since casing was ignored with the "i" flag // Capture Groups match("acd bce", "b(c.)", "", 1); // returns "ce" - the whole match was "bce", but the first capture group is (c.) // Multiple matches matchAll("acd bce", "c.", 1); // returns "ce" - "c." matches both "cd" and "ce", the last argument (and usage of matchAll) selected the second one.
Date Functions
formatDate(date[, formatString])
Takes a numeric date, and converts it into a human-readable format. This is useful for displaying a date to an agent after having done some numerical manipulation to it.
The date
value is in milliseconds since January 1st 1970.
The formatString
accepts Unicode-standard substitutions.
Examples:
// Default formatting formatDate(946684800000) // 01/01/2000 12:00:00 am (+00:00) // Custom formatting formatDate(946684800000, "eeee, MMMM do yyyy, h:mm:ss a (z)") // Saturday, January 1st 2000, 12:00:00 am (GMT) formatDate(946684800000, "eeee, MMMM do yyyy, h:mm:ss a (xxxxx)") // Saturday, January 1st 2000, 12:00:00 am (+00:00)
formatDateISO(date)
Takes a numeric date and converts it into a formatted date string, in ISO 8601 format. This is useful for when you are working with date components or sending the date out to an API call.
The date
value is in milliseconds since January 1st 1970.
Note: The resulting string will be in the agent’s time zone.
Examples:
formatDateISO(946684800000) // 1999-12-31T19:00:00-05:00
dateToMilliseconds(date)
Takes a string date, and converts it into a number of milliseconds since January 1st 1970, for numeric manipulation.
The provided date is expected to be in the default format used by script variables.
Examples:
dateToMilliseconds("01/01/2000 12:00:00 am (+00:00)"); // 946684800000 formatDate(dateToMilliseconds({{Scripter.Customer Call Start Time}}) + 5 * 60 * 1000) // Five minutes (multiplied by 60 sec/minute and 1000 ms/sec to convert to ms) after the customer was connected
formatDuration(duration)
Converts a numeric duration, in milliseconds, into a human-readable format.
Example:
formatDuration(5 * 1000 * 60 + 55); // "00:05:55"
durationToMilliseconds(durationString)
Converts a string duration into a numeric value.
Example:
durationToMilliseconds({{Scripter.Customer Call Duration}}) // How long has the customer been connected formatDuration(5 * 1000 * 60 - durationToMilliseconds({{Scripter.Customer Call Duration}})) // Countdown until the customer has been on the line for five minutes