Actions that can be performed in triggers with fields
Copy the link to the article
Copied

When performing the required action, a trigger can undertake various interactions such as the changing of fields, mathematical operations, and work with functions and PipeLanguage filters.

Mathematical operations in a trigger action

In the trigger action, you can use arithmetic operators such as addition, subtraction, multiplication, etc. The entire list can be found in the article Language of expressions.

Let's analyse a common use-case involving the adding of a markup to the delivery cost using a trigger, that will be executed when the information in the "Prime cost of the delivery" field changes.

Note

For this trigger to work correctly, the integration module with the delivery service must be configured in such a way that the delivery cost, according to the tariff from the delivery service, is added to the "Prime cost of the delivery" field in the order card.

changeSet.hasChangedField("delivery_net_cost") and changeSet.newValue("delivery_net_cost") != null 

In the trigger action, you must select the change of the "Delivery cost" field, and specify in the expression:

order.deliveryNetCost+100

This code will add a markup of 100 euros to the prime cost of the delivery that the delivery service provided for the online store.

Using functions in trigger actions

In PipeLanguage, there are functions and additional functions that can be used in action, as well as a trigger condition. For example, consider a fairly popular situation involving changing the responsible manager in an order to a specific user in the system.

First, specify in the trigger condition:

changeSet.isCreate() and order.orderType.code == "eshop-legal"

This code will work if the system receives an order with the "Legal entity" order type, which in this example is processed by a specific manager, with the identifier "4".

Note

The manager's ID is displayed in the address bar of the browser while being in the user card.

Add an action to change the responsible order manager and specify in the expression:

entity_by_id('User', 4)

This trigger will assign the user with the identifier ("4") as the responsible manager of the order, if the system creates an order for a legal entity.

Working with arrays in a trigger action

Working with arrays of data in PipeLanguage, as with other languages, differs from working with ordinary objects and their parameters. Filters are available for working with arrays, which can be found in the previously mentioned Lanuage Expressions article.

Consider a trigger that will calculate the purchase price of products in an order when you add or remove products, and add or subtract the purchase price from the required field.

Let's create a custom field in which the prime cost of the products calculated by the trigger will be recorded.

First, create a trigger with the following condition:

changeSet.hasChangedField("order_product")

In the trigger action, select the order change, and from the drop-down list of fields, select the previously created custom field and specify the following code in the expression:

order.availableOrderProducts | reduce( (sum, x) =>sum + x.purchasePrice * x.quantity )

Having implemented this process, we will see how much money we spent on products for this order in a separate custom field .

Thank you for your feedback.
Was this article helpful?
No
  • Рекомендации не помогли
  • Нет ответа на мой вопрос
  • Текст трудно понять
  • Не нравится описанный функционал
Yes
Previous article
How to use a trigger to check field changes.
Checking a field in a trigger is necessary if you need an event to be executed only if a certain condition is met. For example, the content of certain digits in a phone number.
Next article
How to avoid the repeated execution of a trigger
This article describes the possible reasons for the repeated execution of a trigger for the same condition.