Skip to content

Function Node - Automate Function Execution

A Function node lets you write and execute custom scripts using JavaScript or Python, or use custom functions within a script you have imported and deployed within the tools automation flow.

Add and Configure a Function Node

Setting up a Function node includes adding it at the appropriate location in the flow and configuring its properties.

Steps to add and configure the node:

  1. Log in to your account and click Tools under Agent Platform Modules. access tools

  2. Click the Tools tab on the top navigation bar, and select the tool to which you want to add the node. The Tool flow page is displayed.

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

  4. 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.)
  5. Click the added node to open its properties dialog box. The General Settings for the node are displayed.
    Configure Function Node

  6. Enter or select the following information:

    • Node Name: Enter an appropriate name for the node based on its functionality or purpose.

    • Select one of the following options to define and execute a function within the node:

      • Write Code: Write a custom code in the built-in editor for the function you want to execute. Learn more.
      • Custom Function: Use a custom function from an imported and deployed script. Learn more.

      For the above options, you can define a script in JavaScript or Python with specific logic, static or dynamic input arguments, and output values.

  1. Click the Connections icon in the left navigation and select Go to Node for success and failure conditions.
  2. Configure Connection Settings
  • 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.
  • 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.
  1. 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.

Define and Execute a Function For The Node

The node provides two options to define and execute its function:

Using Write Code

To write a custom function code from scratch (define its logic and flow), follow the steps below:

  1. Select the Write Code option and click the Expand icon to open the script editor. define a script window

  2. Follow the steps below to complete the process.

    • Select the required coding format in the script editor. coding format

    • 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

    • Click Run in the script editor to test the function. 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

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.

Execute a Custom Function

Selecting Custom Function invokes a function from an imported and deployed script when running the node flow. The steps to set it up are summarized below:

  1. Step 1: Select a Script.
  2. Step 2: Select a Function from the Script.
  3. Step 3: Map the Input Arguments..
  4. Step 4: Test the Script and Function Configuration.

Step 1: Select a Script

To select a custom script deployed in your account, follow the steps below:

Note

The deployed scripts are listed under Settings > Manage custom scripts. Learn more.

  1. Select the Custom function option for the Function node.
  2. Select a deployed script from the list to invoke its function by specifying the Script name. select deployed script

If no scripts are deployed, the following message is displayed.

no scripts deployed

To deploy a custom script, follow the steps below:

  • Click Deploy custom scripts.
  • The system navigates to the Settings > Manage custom scripts page.
  • Follow the steps mentioned here to deploy a custom script.

Once an existing or new script is deployed (after a project is imported), it appears in the Script name list for the Function node.

Step 2: Select a Function from the Script

To select a function the node must execute, follow the steps below:

  1. Choose the function the node should invoke from the list for Function name. select function

    Note

    • All the functions defined in the main file are automatically listed in the dropdown.
    • Only one function can be selected at a time.
    • Functions from undeployed or draft script versions cannot be selected. Only deployed scripts are supported.
    • You can look up a specific function using the search option.
    • search function

When you select a function, the Input Arguments section appears. Arguments are automatically detected from the script and filled in the UI for you if specified in the function. If not, you must add values for each input parameter defined in the function, as discussed below.

Step 3: Map Input Arguments

The next step is to map input arguments of the selected function to static or dynamic values, as discussed below.

Important

By default, all arguments passed to the function are currently sent as 'string'. If your function requires other data types, please handle the necessary conversions within your custom script. Support for input type validation and native data types will be available soon.

Key Considerations

  • Input parameters of the function in the script’s main file are automatically detected and displayed as fields in the UI.
  • You can assign either static or dynamic values to the input fields using context variables. Use the format mentioned here for dynamic values.

    Static values

    Type the values in the text field.

    static values

    Dynamic Values

    To map dynamic values, type the context object format and select the appropriate variable(s) from the suggestions, as shown in the example below. dynamic values

    Note

    • When double curly braces “{{“ are typed in the value field, suggestions for context objects appear.
    • These suggestions list all context objects available for the flow in a list.
    • You can also search from the list to select.
    • Click + Add to add more input arguments and values (key-value pair), and the Delete icon to remove.
    • add and delete

    Note

    The Add button lets you dynamically pass arguments to your function. If the function doesn't support additional arguments, it will fail.

  • Input argument mapping is required for deployment. You can test the function and tool, but can’t deploy until the mapping errors shown below are fixed. input validation error

Step 4: Test the Script and Function Configuration

To test the custom function configuration, follow the steps below:

  1. Click the Test button in the General Settings panel.

    click test

  1. In the Input panel, enter values to test the code. Configured values appear by default, but you can edit or reset them as needed.
  2. input panel
  3. Click Execute to run the function with the configured input arguments.

Results Panel

In this example, for a Banking tool flow, the appendCustomerName function from the GenerateFullName script is executed with firstName and lastName as input arguments. The function combines them to generate the full name. After execution, the following details appear in the Results panel:

Input

All the configured input parameters and their values are displayed in this section.

Edit Input

To edit inputs for the function and re-execute tests, click Edit input. edit input

Output

From the Output API response, the result (of the function code) and function run ID keys from the container are shown in this section. The time taken to generate the output is also displayed.

Click the Copy icon to copy the output.

Success Scenario

success scenario

Error Scenario

error scenario

Key Considerations

  • Successful API calls return output values from the script.
  • The function’s result key value from the script is saved to the function node’s output (End node) as {{context.steps.functionnodename.output}}.
  • Errors are displayed in the panel if an API request fails. The error logs are also displayed. error logs

  • The function’s error/stderr value from the script is saved to the function node’s output (End node) as {{context.steps.functionnodename.error}}.

Logs

The Logs section displays success and error logs from function execution to support debugging.

Values under the stdout and stderr keys are shown here. logs screen

After testing the custom function, the final step is to run and test the complete flow. Learn more.

Test the Node Flow

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

Step 1: Add 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. Manually enter the required static input arguments and their values in the script editor. 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

Step 2: 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

Step 3: Run the Tool Flow

To run and test the tool 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.