This piece of code is saved at the environment level, so you can use it multiple times in a flow, or reuse it in all of your flows. When you update the code, it affects every flow that you added it to. This is different from the Function connector’s Custom Function capability, which only exists once in a single flow.

Because this code is reusable, it can help in the centralization or automation of your organization's common processes.

Setup

Setting up the Code Snippet connector configuration

In DaVinci, add a Code Snippet connection. For help, see Adding a connection.

Connector configuration

Code Snippet

Enter your own JavaScript code within the default module.exports. This gives you access to the variables from your flow.

Tip:
  • To give yourself more room to work, click the Maximize (The Maximize button.) button.
  • To access a variety of useful coding tools, right-click the field.
    A screenshot of the Code Snippet field.
Input Schema

This defines the structure of the data that your code receives from the flow. When you pass variables into your code from the flow, use this space to define whether it is a string, boolean, or other type. For help, see JSON-Schema.org.

Output Schema

This defines the structure of the data that your code returns to the flow. Use this space to define the type for any variable your code returns.

Get your output schema:

  1. Copy your raw JSON output:
    1. Following your Code Snippet node, add an HTTP connector and select the Custom HTML Message capability. Click the new node in your flow.
    2. In the Message field, click {} and insert the output (object) variable from your Code Snippet node.
    3. Click Apply.
    4. Click Save, Deploy, and Try Flow.
    5. Copy the JSON payload that appears in your HTML message.
  2. In a browser, open the Online JSON Schema Validator and Generator tool.
  3. Paste the JSON response you copied into the JSON field. Click Generate Schema From JSON. Copy the output from the JSON SCHEMA field.

    A screen capture that shows the JSON Schema Validator and Generator tool after generating the JSON schema.
  4. In your Code Snippet node, paste the JSON schema into the Output Schema field.

To edit your Code Snippet after adding it to a flow, come back to these details by clicking the Edit button.


A screen capture of the Code Snippet settings, showing the Edit button.
CAUTION:

Any changes you make will affect every flow that uses this Code Snippet.

Using the connector in a flow

Showing a dynamic message with a switch

In this example, a user has selected a multi-factor authentication (MFA) method. Depending on which method they chose, we’ll display a different message on the screen.

  1. Add a Code Snippet connection to your environment. For help, see Adding a connection.
  2. In the Code Snippet field, enter the following switch code:
    // Write your code here
    // Supported language: Javascript
    module.exports = a = async ({params}) => {
        switch (params.mfaMethod){
            case "SMS":
                return {'HTMLResult': "Enter your phone number"};
            break;
    
            case "EMAIL":
                return {'HTMLResult': "Please enter your email address"};
            break;
    
            default:
                return {'HTMLResult': "No MFA method selected."}
        }
    }
    
  3. This code takes the mfaMethod variable from earlier in our flow and checks the value. If it matches SMS or EMAIL, it returns the matching message. If the mfaMethod variable doesn’t match one of the two options, the code returns the error “No MFA method selected”.
  4. We know that the mfaMethod variable in our flow contains text, so we define it as a string in the Input Schema field:
    {
        "input": {
            "type": "object",
            "properties": {
                "mfaMethod": {
                    "type": "string"
                }
            }
        }
    }
    
  5. Our example code always returns a message in the HTMLResult variable, so we define it as a string in the Output Schema field:
    {
        "output": {
            "type": "object",
            "properties": {
                "HTMLResult": {
                    "type": "string"
                }
            }
        }
    }
    
  6. In your flow, add the Code Snippet connection that you created and select the Snippet capability. No other setup is needed.

Getting a value from a JSON response

In this example, we retrieved information about a user from a directory, including the user’s name, address, phone number, and more. To continue our flow, we need the user ID, which is buried somewhere in the response.

  1. Add a Code Snippet connection to your environment. For help, see Online JSON Schema Validator and Generator.
  2. In the Code Snippet field, enter the following:
    // Write your code here
    // Supported language: Javascript
    module.exports = a = async ({params}) => {
        console.log('params: ', params)
        const body = JSON.parse(params.body)
        return {
            'userId': body._embedded.users[0].id
            }
    }
    
  3. This code uses JSON.parse to put the usable data into the body object. This is a good starting point when you want to parse a JSON payload.
  4. Next, the code returns a variable called password userId. In our case, the user ID is located at __embedded.users[0].id inside the body object we created. When creating your own code, look at the structure of the payload to determine the path you need.
  5. We know that the id contains text, so we define it as a string in the Input Schema field:
    {
        "input": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                }
            }
        }
    }
    
  6. Our user ID contains text, so we define it as a string in the Output Schema field:
    {
        "output": {
            "type": "object",
            "properties": {
                "userId": {
                    "type": "string"
                }
            }
        }
    }
    
  7. In your flow, add the Code Snippet connection that you created and select the Snippet capability. No other setup is needed.

Using variables in code snippets

You can create dynamic code by including variables in your code snippets. Variables are populated when the flows runs.
Global variables

For global variables, use the following syntax:

{{global.variables.variablename}}

For example, to include the following username variable, include the following:

{{global.variables.userInfo.username}}
Flow instance variables

For flow instance variables, use the following syntax:

{{local.nodeid.output.variablename}}

To find the value for any variable:

  1. Open your flow.
  2. Select a node that has access to the variable you want to use.
  3. In any field, click {} and select the variable.
  4. Use your mouse to hover over the inserted variable.
A screen recording that shows the flow builder inserting a variable in the UI and checking the hover text.

Capabilities

Snippet


Details
Details
Properties
functionArgumentList functionArgumentList
Output Schema
output object
type object