Optional dependencies

An optional dependency is a mechanism that allows scripts in widgets, business processes, and modules to access objects in other sections and user modules without creating a hard dependency on them. This lets you export and import components even if the target company does not have the related sections or modules.

To add an optional dependency, open the script settings, select the section or module you want to access, and assign it a unique name. Then use this name and the Imports global constant in your script code to access objects and functions from the selected section or module.

When configuring a dependency on a module, you can also include its common scripts to reuse common code in other system components.

This article describes how optional dependencies work, how to use the Imports constant, and how to configure an optional dependency step by step.

Key features of optional dependencies

  1. Access by unique name.
     
    An optional dependency is configured for a specific component's script. To access the linked workspace or module, use the unique name assigned when configuring the dependency.
  2. Access to objects and functions through the Imports constant.
     
    A global constant Imports lets you access objects and functions from linked workspaces and modules in your script code.
  3. Independent export and import of components.
     
    Components can be exported and imported between companies even if the target company does not have the linked workspaces or modules. This is particularly useful when developing extensible solutions, as you can add new functionality as needed without modifying existing components.
  4. Handling missing dependencies.
     
    If a linked workspace or module is missing, you can add a dependency check and error handling to your script.

Add an optional dependency

When you configure an optional dependency in a script, you select which workspace or module you want to access and give it a unique name. This name is then used in the script code to refer to the data and objects of the selected workspace or module. When configuring a dependency on a module, you can also include its shared scripts and assign each one a separate unique name.

Read more about how to create optional dependencies in different system components in the following articles:

After adding an optional dependency, the global constant Imports becomes available in the script. It can be used to access objects of the selected workspace or custom module.

Use the Imports global constant

When you add an optional dependency, you gain access to the global constant Imports. This constant allows you to work with objects within the section, for example, using a script to add a user to the portal or create an application item. Interaction with the module can be done through its configured API methods and actions in business processes.

An example of using involves calling an API method that is in a different workspace in the system:

if (Imports?.documents) {
    let result = await Imports.documents.api.validate.call();
    // ...
}

When writing a script, keep in mind that a component using the Imports constant can be imported even if the dependency is not found in the company. It is recommended to add a block of code that is executed if the selected workspace or module cannot be accessed. For example, to show a clear error message to the user.

In addition, a script component and a workspace or module with which an optional dependency is set can be exported together as part of a solution.

Read more about exporting components with global constants in Global constants in scripts.

Example of using an optional dependency

Let’s say a company has the Payment Documents workspace with the Invoices app and the Documents workspace with the Contracts app. Users need to be able to add new invoices from the contract page by clicking a button.

The workspaces are part of different solutions and need to be exported separately.

In this case, you can set up the button to execute a script, set an optional dependency for the component, and access the app from another workspace using the Imports constant. To do this:

  1. Open the Contracts app page in interface designer and click the Settings > Available items tab.
  2. Next to the Optional dependencies (available in the Imports object) field, click + Add workspace and select the Payment Documents workspace.
  3. In the Alias column, set a unique name for the workspace that will be used in the script: payment_docs.
  4. Go to the Template tab and add a button with the Script type activity to the form. In the Executed script field, create a function that will be called when a user clicks the button.

optional-dependencies-1

  1. Open the function on the Scripts tab and write:

async function create_invoice(): Promise<void> {
    if (Imports?.payment_docs) {
        const item = Imports.payment_docs.app.invoices.create();
        // ...
        await item.save();
    }
}

  1. On the top panel of the interface designer, click Save and Publish to make the changes available to users.

Now users can quickly create a new invoice from the contract page.

You can export the Contracts app, the Documents workspace, and the solution they are part of. After that you can import these components into another company. If the company does not have the workspace with which an optional dependency is set, the script linked with the button will not be functional.