Tech Tip: How to Restore Changes After Unsuccessful Record Save
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: September 23, 2024
The .save() function uses optimistic locking. Locks are not applied before accessing a record and if there was a conflict it is only checked after the access has been attempted. The .save() function checks the stamp on the record and if it has been changed while it was being used the .save() returns unsuccessful and the programmer needs to decide what happens if these conflicts happen. Below is an example of an implementation where after an unsuccesful save some changes to a record, those changes are restored.
$info:=$entity.save(dk auto merge) Case of : ($info.success) // save successful : ($info.status=dk status automerge failed) // save failed, reload var $touchedAttributes : Collection $touchedAttributes:=$entity.touchedAttributes() var $old : 4D.Entity $old:=$entity.clone() $entity.reload() For each ($touchedAttribute; $touchedAttributes) $entity[$touchedAttribute]:=$old[$touchedAttribute] End for each End case |