In BRIX, you can add items and update their fields in batches using batch methods in scripts written in the TypeScript language. This allows you to reduce the load on the system and the impact of set limits when you need to process a large amount of data. For example, if you have an integration set up with a third-party system, you can import a product catalog into the app from that system.
You can set the following methods in scripts:
app.batch().save()
to add app items in bulkapp.batch().update()
to update fields of app items in bulk
начало внимание
These methods are available in BRIX SaaS edition. They will also be available in the BRIX On‑Premises edition in upcoming releases.
конец внимание
Please note, no more than 10 000 items are processed per request. This limitation applies to all the specified methods.
In this article, we will look at examples of how to use methods in scripts.
Add items in bulk
With app.batch().save()
, you can process a batch of app items in a single request.
Use this method to:
- Save new items to the app
- Update the fields of existing items. In this case, specify their identifiers so that you can use them to find the required items, make changes to them, and save the edited items in the database.
Use case 1
Let’s assume that a company uses the BRIX system, and marketing employees also work with a third-party service that stores product publications and customer comments.
You can import information from this external service to BRIX in bulk allowing employees from related departments to view all data in one place. For this:
- Create apps for uploading publications and comments.
- Set up integration via a custom module and add two script buttons to the module interface:
- Add Posts to import all publications from a third-party service to BRIX
- Add Comments to upload all comments to these publications
Step 1: Create two apps
First, create apps to import data into them. As you do so, add properties to the apps to match the fields of the publications and comments in the third-party system. Here is an example:
- Posts. Contains the following properties:
- Name (
__name
). A system property, created automatically in BRIX. - Number (
postID
). The Number (integer) type for the identification number, by which the external system determines the link between the publication and its comments, i.e. the values of this field are equal in related items. - Title (
title
). The String (string) type for the title of the publication. - Text (
body
). The String (text) type for the text of the publication. - Comments
comments
. The App (one) type for a link to a related comment.
- Comments. Contains the following properties:
- Name (
__name
). A system property created automatically in BRIX. - Number (
postID
). The Number (integer) type for the identification number by which the external system determines the link between the publication and its comments, i.e. the values of this field are equal in the related items. - User's e-mail (
email
). The Email address type for the email of the user who left the comment. - Message (
body
). The String (text) type for the text of the comment. - Posts
post
. The App (one) type for the link to the linked post.
These properties will be used in the script to define the fields where the values sent from the external service will be written. Read more about filling in the Comments and Posts fields of the App (one) type in Use case 2.
Step 2: Configure data loading via a script
After creating the app, configure data import from the external system.
Here is how:
- In the configured integration module, go to the Settings tab and click Change Form.
- In the interface designer that opens:
- Create the Add Posts and Add Comments buttons
- On the Scripts tab, write Client-side code using
app.batch().save()
. This code will import data into the app when the user clicks the Add Posts or Add Comments buttons.
/* Client scripts module */ |
- Save and publish the changes.
The buttons will then appear in the settings of the integration module. The user can now click them to add new items to the Posts and Comments apps.
Batch update of item fields
To update multiple item fields in a single request, use the app.batch().update()
method in the script. This allows you to:
- Specify the rules for such an update
- Define search criteria for items to be updated
- Limit the number of items to be updated
Use case 2
In Use case 1, we transferred data from the external system to the Posts and Comments apps using the app.batch().save()
method in the script. However, the Posts field in comment pages and Comments field in post pages are still empty. The links for these fields are not transferred from the external system, but are created in BRIX.
Let's fill these empty fields with links to related items so that employees can quickly find the necessary information when working in these apps.
To do this, we will use the app.batch().update()
method to update the fields in bulk.
In the third-party system, the link between publications and comments is defined through the Number (postID
) field. The value in this field is the same in all the linked items. We will use this property in the script to find the required publications and add links in the Posts field to comment pages.
To do this:
- In the customized integration module, go to the Settings tab and click Change Form.
- In the interface designer that opens, add the Button widget and name it Update Links.
- On the Scripts tab, write the code that will update the links between the items of the Posts and Comments apps. When the user clicks the Update Links button, the links in the Post fields will be updated in the comment pages.
//Asynchronous function to update the Posts field in comments |
- Save and publish the changes.
After that, the Update Links button will appear in the settings of the integration module. Now, users can click it to update the Posts field in the comment pages.