Global context and isolation

During a script’s execution, several global constants can be called.

Context

The main global constant is the Context object. Its content depends on the location of the script. For example, in a process script, Context contains information about the instance of this process.

Context is an object with a list fields determined by the user (read more in Manage types). For example, if you add a String type field with the code foo to the Context tab, you’ll be able to work with it in the following way:

Context.data.foo = 'some string';

Context is much like an app item object, except that it has no methods for saving or deletion. It is saved automatically when the script finishes executing.

Application

If a process or widget is associated with an app, the Application object can always be accessed in it. This global constant provides methods to work with the current app. For instance, you can create a new order in the following way:

const order = Application.create();
order.data.client = Context.data.client;
await order.save();

Namespace

If a process or widget is associated with a workspace, it may have access to the Namespace object that contains all apps stored in the workspace. This allows you to get the description of a specific app, for example, Namespace.app.orders:

const clientOrders = await Namespace.app.orders.search()
    .where(f => f.client.eq(Context.data.client))
    .all();

Global

You can also call the Global object in your scripts. It contains the description of all workspaces. If you use this object, the export of the script will be forbidden. This means that if you use Global in a script of a process or a widget associated with a workspace or an app, you won’t be able to export this workspace or app.

The Global object contains all available workspaces in the ns field. This means that if you create the store workspace and the orders app inside of it, you can get the app’s description using Global.ns.store.app.orders:

const clientOrders = await Global.ns.store.app.orders.search()
    .where(f => f.client.eq(Context.data.client))
    .all();

Read more about working with apps in the Manage apps article. The Global constant is always available in global processes and widgets. In processes associated with apps or workspaces, you need to manually allow access to this constant.

Starting processes

You can also use the Namespace and Application objects to start processes.

await Application.processes.process_code.run({param: 1});
await Namespace.processes.process_code.run({param: 1});
await Global.ns._clients.app._leads.processes.process_code.run({param: 1});
await Global.ns._clients.processes.process_code.run({param: 1});

For more information, see the description of the Process object.

System

Using the System object, you can access such system collections as:

Server

In client-side widget scripts, the Server global constant can be called. In the Server object, methods from the widget’s server-side scripts are available.

await Server.rpc.doSomething()

Read more in the Scripts in widgets article in the Help Center.