CVP Call Studio - JavaScript (Set Value node)

Create Local variables (similar to session variables) by using the JavaScript node “Set value” node.

CVP’s JavaScript engine (Rhino) runs on the JVM. That means every Java class is accessible from your JS code using the Packages namespace.

Example: java.security.MessageDigest becomes Packages.java.security.MessageDigest in JS. So in the below code, it is calling the Java MessageDigest.getInstance method to get a SHA-256 digest object. You can instantiate objects, call methods, and access constants just like in Java.

Example JS function which you can use in a Set Value node - to get the SHA256 of a value:

SHA 256 Example

// CVP Rhino: SHA-256 via Java MessageDigest
function sha256_sql(accountNum) {
    if (!accountNum) return null;

    // Convert JS string to UTF-8 bytes
    var javaStr = new java.lang.String(accountNum);
    var bytes = javaStr.getBytes("UTF-8");

    // Create SHA-256 digest
    var md = java.security.MessageDigest.getInstance("SHA-256");
    md.update(bytes);
    var digest = md.digest();

    // Convert byte array to hex string
    var hex = '';
    for (var i = 0; i < digest.length; i++) {
        var h = (digest[i] & 0xFF).toString(16);
        if (h.length === 1) h = '0' + h;
        hex += h;
    }

    return hex.toUpperCase();
}

// Example usage
var accountNum = "4111111111111111";
var hash = sha256_sql(accountNum);

// Expected output: 9BBEF19476623CA56C17DA75FD57734DBF82530686043A6E491C6D71BEFE8F6E

Retrieving the Length of a Session Variable in Call Studio

var str = {Data.Session.pin};
eval(str.length);

Converting a double to a Integer

var str = {Data.Session.MyDoubleValue}
parseInt(str, 10);
var a = 5;
var b = 3;
var c = a+b;
eval (c);

removing first character from a string

var str = {Data.Session.var1};
eval(str.substring(1));

Removing leading digit of phone number and adding +353

var enteredNum = {Data.Element.Digits_EnterPhoneNum.value};
var e164Num = '+353' + enteredNum.slice(1);
e164Num;

So do no use eval - actually for any value - you do not need to use the eval - and I believe it is better not to. eval is used in the cisco examples. Just return the string itself.

var pollDurationInt;
pollDurationInt = {Data.Session.pollDuration};
pollDurationInt = parseInt(pollDurationInt,10);
pollDurationInt = pollDurationInt .toString();
pollDurationInt;