Set of changes

Features of work with set of changes

Object changeSet (type Change\EntityChangeSet) presents interface of getting information on changes when the object is edited (for example, order or customer). In reference book of objects you may see available methods of this object.

The most commonly used methods are hasChangedField(), getOldValue() и getNewValue(). Using hasChangedField() it is possible to define if the field was changed, and getOldValue() and getNewValue() allows to get accordingly old and new value of field.

Example:

changeSet.hasChangedField("status") and changeSet.getNewValue("status").getCode() == "did-not-get-through"

These methods take as a parameter working field name in the underscore format. If we refer to inserted field, then nesting is indicated through a dot.

Examples:

changeSet.hasChangedField("status") # Defines if the field Status of order was changed
changeSet.getNewValue("delivery_cost") # Gets new value of the field Delivery cost
changeSet.getNewValue("delivery_address.city") # Gets new value of the field City of delivery
changeSet.hasChangedField("delivery_type") and changeSet.getNewValue("delivery_type").getCode() == "russian-post" # Defines if the delivery type was changed to "Russian Post"

In these examples we can see the changes of fields:

order.getStatus()
order.getDeliveryCost()
order.getDeliveryAddress().getCity()
order.getDeliveryType()

Changes in custom fields are stored with prefix custom_. For example, to define if the custom field transaction_id was changed, needed to write:

changeSet.hasChangedField("custom_transaction_id")

It is important to understand the verification difference:

changeSet.hasChangedField("delivery_type") and changeSet.getNewValue("delivery_type").getCode() == "russian-post"

from

order.getDeliveryType() and order.getDeliveryType().getCode() == "russian-post"

The first condition works only at the moment when we change delivery type on "Russian Post". The second condition will work at the moment of delivery type change to "Russian Post" and also with all subsequent changes in order until we will change delivery type on any another value.

To find out what action is performed with the object now (creating, editing, deleting), in changeSet there are according methods:

changeSet.isCreate() # creating
changeSet.isUpdate() # editing
changeSet.isDelete() # deleting

Getting of change source

Sometimes it is necessary to get the manager, which changed any field, for example it is possible to appoint responsible manager if he made some changes in order items.

To find out which manager changed the field, needed to use function getAuthorOfChange, which returns the manager who made changes (User) or null if change was made by system, by API or by trigger.

changeSet.getAuthorOfChange("status")
changeSet.getSourceOfChange("status") in ["code", "user", "api", "rule"]

Also it is possible to check any changes by definite source:

changeSet.hasChangesWithSource("api")
changeSet | contains ( c => c.fieldName == 'order_product.status' and c.newValue.code == 'otklonen' and c.oldValue.code == 'new')

Tracking product changes in order

Since there can be several products in the order, you should use filters for working with collections if it is required to check fields changes correctly.

For example, changes of a product status are checked by:

changeSet | contains ( c => c.fieldName == 'order_product.status' and c.newValue.code == 'canceled' and c.oldValue.code == 'new')

PrintEditHistory
Page last modified on May 21, 2019, at 03:11 PM