KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Duplicating Entity in ORDA
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: January 18, 2021

In classic 4D, duplicating records could be done using the DUPLICATE RECORD command. What is the ORDA equivalent? In ORDA, entities can be duplicated with the help of toObject(), fromObject(), and OB REMOVE.

toObject() is used to create an object built from an entity and fromObject() is used to fill the new entity with the object contents. Note that it is important to remove the primary key from the object before filling the new entity with fromObject(). Removing the primary key will allow 4D to generate an auto-incremented primary key for the duplicated entity. If the primary key is not removed, an error will occur when attempting to save the entity with an already existing primary key. The OB REMOVE command can be used to remove the primary key from the object.

Here is an example of duplicating an entity in ORDA

C_OBJECT($entity_original;$ob;$entity_new;$status)

//Retrive an entity
$entity_original:=ds.TableName.get(1)

//Duplicate entity and remove the primary key
$ob:=$entity_original.toObject()
OB REMOVE($ob;"ID")
$entity_new:=ds.TableName.new()
$entity_new.fromObject($ob)

//Save duplicated entity
$status:=$entity_new.save()