KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Retian Entity Data after Drop
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: January 18, 2019
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

The entity.drop( ) method deletes the data contained in the entity from the datastore. The record referenced by the entity is also deleted from the related table. This entity method performs the same action as DELETE RECORD, with one notable difference: the deleted data remains in the memory and can still be referenced after deletion.
We can utilize this feature to help user needs review last change, or even recover deleted record. For example, the code below retrieves first record from the database as an entity, drop it, and restored it using the reference retained in memory:

C_OBJECT($person;$personObj;$result)
$person:=ds.Person.get(1)
$result:=$person.drop()
If ($result.success=True)
  $newPerson:=ds.Person.new()
  $newPerson.fromObject($person.toObject())
  $status:=$newPerson.save()
End if

Note $person is copied to $newPerson with object using toObject() and fromObject. If the refence of deleted $person is direct cloned to $newPerson, it will have the previous stamp, which will result a failure when trying to save $newPerson. In order to recreate a record with same primary key, the stamp needs to start from scratch.