Skip to content

Function Node - Automate Script Executions

Function nodes let you write custom scripts using JavaScript or Python to process context variables or other variables used in the tool flow.

Add and Configure a Function Node

Setting up a Function node in a tool flow involves adding the node at the appropriate location in the flow and configuring various node properties.

Steps to add and configure the node:

  1. On the Tools tab, click the name of the tool to which you want to add the node. The Tool flow page is displayed.

  2. Click Go to flow to edit the in-development version of the flow.

  3. In the flow builder, click the “+” icon on any existing node on the canvas and select Function from the pop-up menu. (Alternatively, drag the Function node from the Assets panel onto the canvas.)

  4. Click the added node to open its properties dialog box. The General Settings for the node are displayed.
    Configure Function Node

  5. Enter or select the following information:

    • Custom Name: Enter an appropriate name for the node.

    • Define a Script: You can define a script using Javascript or Python to process/parse the given input or the output of the previous node. Click anywhere in the Define a Script field to open the Script Editor dialog box.
      Configure Function Node

      1. In the top-left corner, choose the appropriate scripting language - Javascript or Python. Enter the script in the box. You can use variables or context variables in the Script Editor. For example, to read an input variable, use the following syntax:

        context.<variable-name> = context.steps.<startNode-Name>.<inputVariable-name>
        
        To call a function, use the following syntax:
        context.<UserDefined-Variable-Name>= UserDefined-Function-Name(context.steps.<startNode-Name>.<inputVariable-name>)
        

        Note

        You cannot import packages in the Function node. However, you can use pre-existing libraries such as pandas or numpy. Learn more.

      2. You can use the Context input or output feature to use a variable from the previous node or when testing a function.

        • Context Input: Add the required variable; dummy input values can be given to test the defined function.
        • Context Output: Shows the results of the function from the given input.
        • Log: Monitor the state of the function and view the values.
      3. Click Run to test the script. Once the script is resolved successfully, the results are displayed in the Log section.

      4. Close the Script Editor.
  6. Click the Connections icon in the left navigation and select Go to Node for success and failure conditions.
    Configure Connection Settings

    1. On Success > Go to Node: After the current node is successfully executed, go to a selected node in the flow to execute next. For example, you can go to an AI node to use the processed data from the Function node.
    2. On Failure > Go to Node: If the execution of the current node fails, go to an appropriate node having a custom error message configured for this node.
  7. Finally, test the flow and fix any issues found.

Standard Errors

You can see compilation and runtime errors, if any, during the execution of the script/node.

Test the Flow

After adding and configuring the node as mentioned here, follow the steps below to test the flow:

Step 1: Define and Test the Function Code

To define the logic and flow of the script, follow the steps below:

  1. In the Define a Script window, click the Expand icon to view and edit your code in the script editor. define a script window

  2. Select the required coding format. coding format

  3. Use these syntaxes to define the code in JavaScript or Python. You can add static or dynamic input variables in the code to generate the output. dynamic context

  1. Click Run in the script editor to test the code.
  2. run script

The script editor has the following tabs representing the code components:

  • Context Input: Displays the context input(s) fetched from the Start node or the static inputs in the code. context input

  • Context Output: Displays the output generated by the script. context output

  • Log: Displays the code execution log, including the output or error(s). logs

Using Static or Dynamic Values in the Script

Define Static Input Variables

  1. In the script editor, select the coding format from the dropdown.
  2. Define the input variables and their values as shown below. static code

Define Dynamic Input Variables

  1. In the script editor, select the coding format from the dropdown.
  2. Define the input variables and define dynamic values using context variables in the defined format, as shown below.

JavaScript

dynamic inputs js

Python

dynamic inputs py

Step 2: Test the Flow using Static or Dynamic Inputs

Static Inputs

To run the flow for static inputs, follow the steps below:

Note

In this case, you do not need to add input variables using the Start node.

  1. Add/edit your script in the editor by defining static variables. Learn more.
  2. Click the Run Flow button at the top-right corner of the flow builder.

Dynamic Inputs

To run the flow for dynamic inputs, follow the steps below:

  1. Click the Input tab of the Start node, and click Add Input Variable to configure the input for the flow’s test run. Learn more.

add input variable

  1. Add the Name(key) value, select the data type for Type, and provide a description in the Enter input variable window. For example, in the banking flow, to get the sum of two balances, one in Q3 and the other in Q4, you must define two input variables, “Q3balance” and “Q4balance,” as shown below. add input var
  2. Click Save.

Once you define the input variables, you must add the output variable(s) and run the flow.

Important

  • You can use the Start node’s input variables as context variables in the script editor to accept dynamic values and generate the output. To refer to the input variable, follow the syntax mentioned here.
  • Once you run the node’s flow, the result gets stored in the output variable of the Start node. Additionally, this key is mapped to the End node, where you can define its value.
  • end node key

Add the Output Variable

To define the output variable, follow the steps below:

  1. Select the Start node and click the Output tab.
  2. output tab
  1. Click Add Output Variable.
  2. Enter the value for Name (key) and select the data type for Type.
  3. Click Save.
  4. save output variable

Run the Flow

To run and test the flow, follow the steps below:

  1. Click the Run Flow button at the top-right corner of the flow builder. run flow

  2. (Optional) Provide input to test the flow if you have configured it in the Start node. Otherwise, go directly to the next step.

  3. Click Generate Output.

    generate output

The Debug window generates the flow log and results for the given input(s), as shown below. Learn more about running the tool flow.

debug log

Access the AI Node’s Output

The node’s output is stored in a context variable. You can access the variable using the syntax: {{context.steps.&lt;<Functionnodename>>.output}

For example, context.steps.Bankingnode.output

Note

Agent Platform can automatically recognize variables and outputs. To do so, type "context.steps." and you will see the available variables, nodes, and node outputs.

Context Variables for Dynamic Inputs

Before you run the flow, provide clear instructions for the model to follow by adding the input variable(s) using context variables. Context variables allow you to include dynamic values in the script that a node executes to generate its output. The JSON code editor supports both JavaScript and Python formats.

Syntaxes for the Context Input

JavaScript

The recommended syntax to fetch dynamic variables using JS in the context input is: {{context.steps.Start.variable-name}}

For example, context.steps.Start.Q3balance

Python

The recommended syntax to fetch dynamic variables using Python in the context input is: {{context["steps"]["Start"]["variable-name"]}}

For example, context["steps"]["Start"]["Q3balance"]

The above syntaxes fetch the variable “Q3balance” that you define in the Start node. Learn more.