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.
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.
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:
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.
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".
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:
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 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:
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 .