You can use the Functions connector to:

  • Compare the value of variables in your flow, such as A > B,
  • Perform calculations
  • Run custom JavaScript

Setup

Setting up the Functions connector

In DaVinci, add a Functions connection. For help, see Adding a connector.

This connector doesn’t have a configuration at the environment level. You configure it in your flow instead.

Using the connector in a flow

Comparing values

You can compare values using a variety of comparison capabilities, such as A == B, A != B, and A < B < C.

For example, we can check whether a user is eligible for a senior’s discount based on their age:

  1. In your flow, add the Functions connector and select the A >= B capability. Select the node that appears in your flow.
  2. In the Value A field, click {}, then select the attribute that holds the user’s current age.
  3. In the Value B field, enter 65.
    A screen capture of the user inserting variables into the value fields.
  4. After applying your changes, you can add a True branch from your node for seniors, and a False branch for those who aren’t.

Adding a custom function to your flow

You can make your flows more powerful and flexible by adding your own custom code. This capability lets you take inputs from your flow, work with them in JavaScript, and make the results available in your flow.

The JavaScript in a custom function runs on the server, not the client. These functions stay with your flow if you export it to another DaVinci environment.

For example, we want to know how long until a user turns 65 and gets a senior’s discount. We already know their age from earlier in the flow and that they’re younger than 65. Our code will subtract their current age from 65 and make the result available in the flow.

Note:

The following libraries are available in custom functions:

  1. In your flow, add the Functions connector and select the Custom Function capability.
  2. Select the node that appears in your flow.
  3. Make the variable with their current age available in your function:
    1. In the Variable Input List, click Add.
    2. In the Variable Name field, enter currentAge. Our code will call the variable by this name.
    3. In the Value field, click the input button and select the variable from your flow that you want as the value of currentAge in your function.
    4. In the Data Type list, select the type that best suits the value your variable holds.
    5. (Optional) To bring in more variables, click Add again.
      Tip: To remove an extra variable, click Edit, then click the remove button.
  4. In the Code field, enter your JavaScript custom code.

    Tip: To give yourself more room to work, click the Expand (The Expand button.) icon. This lets you use your browser zoom to enlarge the code.

    Our code creates a variable called ageDelta that holds the difference between the user’s current age and 65:

    // How long until eligible for a senior’s discount?
    module.exports = a = async ({params}) => {
        Let ageDelta = 65 - params.currentAge
        return {'yearsUntilDiscount': ageDelta}
    }
    

    Our code returns a variable called yearsUntilDiscount.

  5. In the Output Schema field, capture the return variables from your code and make them available elsewhere in your flow.
    {
        "output": {
            "type": "object",
            "properties": {
                "yearsUntilDiscount": {
                    "type": "integer"
                }
            }
        }
    }
    

    Now, yearsUntilDiscount is available to use elsewhere in your flow.

Checking whether a variable has a value

When building a flow, it can be useful to check whether a variable contains a value. For example, testing whether the expected user ID or token has actually been provided earlier in the flow.

A screen capture of a short flow that branches based on whether a variable is empty.
  1. Check whether the variable is empty:
    1. In your flow, add a Functions connector with the A is Empty capability.
    2. In the Value A field, click {} and select the variable that you want to check.
    3. Enable Check undefined/null.
    4. In the Type list, select the type of data that the variable should contain.
      A screen capture of the A Is Empty capability configuration.
    5. Click Apply.
  2. Add a success path by following the A is Empty node using the Any Trigger False condition.
    Note:

    The A is Empty capability returns "false" when the expected User ID, token, or other value is provided. As a result, the success path for your flow follows branches from the "false" result.

  3. Add an error message or failure path by following the A is Empty node using the All Triggers True condition.

Capabilities

A == B

If A equals B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
A != B

If A is not equal to B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
A < B

If A is less than B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
A <= B

If A is less than or equal to B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
A > B

If A is greater than B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
A >= B

If A is greater than or equal to B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
B < A < C

If A is between B and C

Details
Details
Properties
leftValueA textField
rightValueB textField
rightValueC textField
type dropDown
B <= A <= C

If A is between B and C or equal to B or C

Details
Details
Properties
leftValueA textField
rightValueB textField
rightValueC textField
type dropDown
B <= A < C

If A is between B and C or equal to B

Details
Details
Properties
leftValueA textField
rightValueB textField
rightValueC textField
type dropDown
B < A <= C

If A is between B and C or equal to C

Details
Details
Properties
leftValueA textField
rightValueB textField
rightValueC textField
type dropDown
A is Empty

If A is Empty

Details
Details
Properties
leftValueA textField
Check undefined/null toggleSwitch

Check undefined/null

type dropDown
If Array A includes B

Details
Details
Properties
leftValueA textField
rightValueB textField
type dropDown
inputContains dropDown
If String A includes B

Details
Details
Properties
leftValueA textField
rightValueB textField
Custom Function

Details
Details
Properties
variableInputList variableInputList
code codeEditor

Follow example for code.

Default:

// Write your code here
// Supported language: Javascript 
module.exports = a = async ({params}) => {
	console.log('params: ', params)
	return {'testVariable': params.testVariable}
}
outputSchema codeEditor

Follow example for JSON schema.

Default:

{
	"output": {
		"type": "object",
		"properties": {
			"testVariable": {
				"type": "string"
			}
		}
	}
}
Output Schema
output object
type object
Create a Hash

Details
Details
Properties
Hash Algorithm dropDown
Message for Hash textField
Input Schema
default object
message string required

Message to digest

digestAlgorithm string required

Algorithm, e.g. SHA256

Output Schema
output object
hash string
A == B (Multiple Conditions)

If A equals B or A equals to C ...

Details
Details
Properties
leftValueA textField
rightValueMultiple multipleTextFields
type dropDown