Manage types

All system objects can be described as one-level structures where codes of fields correlate with their types. All types can be divided into four classes:

There is also a special type used for storing unstructured data, TJSON. The instances of this type are constructed differently depending on the class.

Primitive types

Primitive types are similar to TypeScript primitive types. For example, the context of a process may include a field of the TString type used for the user’s comment. You can work with it in the same way as with a regular string:

Context.data.__comment = `The script has been executed successfully!`;

Structural types

In JavaScript terminology, structural types are basically objects. For example, you can work with a process initiator’s full name (TFullName type) in the following way:

const initiator = await Context.data.__createdBy.fetch();
const fn = initiator.data.fullname; 
Context.data.__comment = `The script was launched by ${ fn.firstname } ${ fn.lastname }`; 

Class instances

Class instances are created with a constructor or field description. Let’s say the context of a process includes a field with the code total of the TMoney type. To fill it out, you need to do the following:

Context.data.total = new Money(1799.99, 'USD');

To add a new row to a TTable type field, do the following:

// Get an order 
const order = await Context.data.orders!.fetch(); 
// Insert a new row into the table
const row = order.data.content!.insert();
// Fill it out
row.item = Context.data.product!;
row.amount = Context.data.amount!;
// Save the order
await order.save();

Please note that the table won’t be updated unless the app item is saved.

Links to objects

Links to system objects have the fetch() method that is used to get the objects themselves. Read more about objects in Types of objects.

Field description

Each type also has a description object that can be used to construct objects or for other operations. For example, if there is a field of the Users (Many) type participants in the context, it will store an array of links TUser. To get an array of users from these links, use the description of the corresponding field of the UserField type. It has access to the UserField.fetchAll method:

const participants = await Context.fields.participants.fetchAll();

If there is an App type field order in the context, use its description to access the app itself, for example, to create a new app item:

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

Types of objects

System objects are entries in a database. These can be app items (ApplicationItem) or basic system objects (UserItem, FileItem, or ImageItem). Each object is described by a set of fields (ItemData) where codes of fields correlate with their types. Objects also have a basic set of fields BaseItemData. Each object contains the description of its own fields (the fields property) and the object of field values (the data property). The description of a field depends on its type.

Links to objects

For any object, there is a link, or reference object, ItemRef. It can be used to obtain the object itself. For example, the basic set of fields of any object includes a link to the object’s author __createdBy. To get the object storing the author, do the following:

const author = await Context.data.__createdBy.fetch();

Any link has the fetch() method that can be used to request the object using a link to it.

Users

A user (UserItem) is a system object with a set of fields UserData. You can reference a user with an object of the UserItemRef type. At the moment, it is impossible to create new users or edit them in scripts.

Let’s say there is a process in the order app. It starts as a new app item is created, and a task needs to be assigned to the author of the item. All you need to do is to create a dynamic swimlane and link it with a new variable author, to which the author of the item is passed:

const order = await Context.data.order.fetch();
Context.data.author = order.data.__createdBy;

For the user object and user reference object, you can use the UserItemRef.getChiefs method that allows you to get a user’s superiors. For example, you can get the superiors of the item’s author from the previous example in the following way:

Context.data.chiefs = await Context.data.author.getChiefs();

Files

A file (FileItem) is a system object with a set of fields FileData. To reference a file, use an object of the FileItemRef type. At the moment, it is impossible to edit files using scripts.

The FileField.create method allows you to create a file in the process context and link it with a Files type field:

const fileRq = await fetch(`https://jpeg.org/images/jpegsystems-home.jpg`)
const buf = await fileRq.arrayBuffer()
const file = await Context.fields.file.create('home.jpg', buf)

The FileField.createFromLink method allows you to upload a file from an external source and add it to the process context, linking it with a Files type field:

const file = await Context.fields.file.createFromLink('home.jpg', `https://jpeg.org/images/jpegsystems-home.jpg`)

The FileItem.getDownloadUrl and FileItemRef.getDownloadUrl methods allow you to get a link to download a file:

const url = await file.getDownloadUrl();

App items

An app item (ApplicationItem) consists of the fields of the app it belongs to. If there is an App type field in the context, it stores one ApplicationItemRef link or an array of these links (if the Many option is selected). You can access app objects using links from other objects or global constants. Learn more about working with apps in the corresponding article.