Example with a change in the stock balance
set, setList and setListExtended can set not a final value in the field, but change it relative to the existing one.
For this, instead of =, + or - is used.
For example, when adding an operation to the warehouse, we can change the stock of an item in the Warehouse table:
=: set(table: 'warehouse'; field: 'count' + $#nfv)
Example with adding values to a list
Similarly, set, setList, and setListExtended can add or remove a value if their target field is a select.
For this, instead of =, + or - is used.
For example, let's add the bases 45, 56, and 17 to list:
=: set(table: 'table'; field: 'list' + json`["45","56","17"]`)
For this to work, the target field must have the multiple values parameter enabled!
How do I know which values were added or deleted when I changed the list?
For example, the list is changed manually, and you need to perform some action on the added values. There is a way to do this through listMinus with old and current values:
=: linkToDataText(title: "Show added values"; text: $join)
join: listJoin(list: $list; str: $#nl)
list: listMinus(list: $#nfv; list: $#onfv)
So, we subtract the old list from the new list in the field, and only the newly added values remain. We use join to display them in linkToDataText.
If you need to get the values removed from the list, you need to swap $#nfv and $#onfv — then only the values that were there before the change and are no longer there will remain.
How to automatically add a record to a field with the type Comment?
In the field comments, you can add a separate message with code.
Add an entry from the current user:
example1: set(table: 'test'; field: 'comment' = "Test comment"; where: 'id' = 3)
Historically, it turned out that in comments adding a message uses the = operator.