Functions Connector
The Functions connector lets you branch your PingOne DaVinci flow using logical conditions or based on the result of custom JavaScript code.
You can use the Functions connector to:
-
Compare the value of variables in your flow, such as A > B,
-
Perform calculations
-
Run custom JavaScript
For best practices to use custom code in your flows, see Using custom code safely. |
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.
A node with the A == B capability cannot be branched from top: |
For example, we can check whether a user is eligible for a senior’s discount based on their age:
-
In your flow, add the Functions connector and select the A >= B capability. Select the node that appears in your flow.
-
In the Value A field, click {}, then select the attribute that holds the user’s current age.
-
In the Value B field, enter 65.
-
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.
The following libraries are available in custom functions:
|
All code for custom functions must comply with JavaScript’s strict mode. For more information, see Strict Mode. |
-
In your flow, add the Functions connector and select the Custom Function capability.
-
Select the node that appears in your flow.
-
Make the variable with their current age available in your function:
-
In the Variable Input List, click Add.
-
In the Variable Name field, enter
currentAge
. Our code will call the variable by this name. -
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. -
In the Data Type list, select the type that best suits the value your variable holds.
-
(Optional) To bring in more variables, click Add again.
To remove an extra variable, click Edit, then click the remove button.
-
-
In the Code field, enter your JavaScript custom code.
To give yourself more room to work, click the Expand () 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
. -
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.
-
Check whether the variable is empty:
-
In your flow, add a Functions connector with the A is Empty capability.
-
In the Value A field, click {} and select the variable that you want to check.
-
Enable Check undefined/null.
-
In the Type list, select the type of data that the variable should contain.
-
Click Apply.
-
-
Add a success path by following the A is Empty node using the Any Trigger False condition.
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.
-
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
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
A != B
If A is not equal to B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
A < B
If A is less than B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
A ⇐ B
If A is less than or equal to B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
A > B
If A is greater than B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
A >= B
If A is greater than or equal to B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
B < A < C
If A is between B and C
Show 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
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- rightValueC
textField
- type
dropDown
B ⇐ A < C
If A is between B and C or equal to B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- rightValueC
textField
- type
dropDown
B < A ⇐ C
If A is between B and C or equal to C
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- rightValueC
textField
- type
dropDown
A is Empty
If A is Empty
Show details
- Properties
- leftValueA
textField
- Check undefined/null
toggleSwitch
-
Check undefined/null
- type
dropDown
If Array A includes B
Show details
- Properties
- leftValueA
textField
- rightValueB
textField
- type
dropDown
- inputContains
dropDown
Custom Function
Show 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